Questions tagged [regex-group]

Regex groups are created by placing part of a regular expression inside parentheses. Groups allows to apply a quantifier to the entire group or to restrict alternation to part of the regex. Besides grouping part of a regular expression together, parentheses also create a numbered capturing group. It stores the part of the string matched by the part of the regular expression inside the parentheses.

The regex Set(Value)? matches Set or SetValue. In the first case, the first (and only) capturing group remains empty. In the second case, the first capturing group matches Value.

If capturing the match isn't needed, the regular expression can be optimized into Set(?:Value)?. The question mark and the colon after the opening parenthesis are the syntax that creates a non-capturing group.

The question mark after the opening bracket is unrelated to the question mark at the end of the regex. The final question mark is the quantifier that makes the previous token optional. This quantifier cannot appear after an opening parenthesis, because there is nothing to be made optional at the start of a group. Therefore, there is no ambiguity between the question mark as an operator to make a token optional and the question mark as part of the syntax for non-capturing groups.

2670 questions
4
votes
1 answer

What is the Regular Expression to get all the newline characters from the end of the string

I have tried with [\s]+$ and (?:$|\s)+$ but i don't get the desired output. What i am looking for is String str ="this is a string ending with multiple newlines\n\n\n" the new line can be : \n or \r or \r\n depending on OS so we use \s+ here. I…
backToStack
  • 101
  • 6
4
votes
3 answers

How to insert a white space before open bracket

I have a string 3.4(2.5-4.7), I want to insert a white space before the open bracket "(" so that the string becomes 3.4 (2.5-4.7). Any idea how this could be done in R?
Patrick
  • 1,057
  • 9
  • 23
4
votes
3 answers

Python Regex remove space b/w a Bracket and Number

Python, I have a string like this, Input: IBNR 13,123 1,234 ( 556 ) ( 2,355 ) 934 Required output- : Either remove the space b/w the bracket and number IBNR 13,123 1,234 (556) (2,355) 934 OR Remove the brackets: IBNR 13,123…
karan
  • 309
  • 2
  • 10
4
votes
1 answer

What does \0 mean in sed?

I have the following code: echo "12. Chapter Name" | sed -n -E "s/([0-9]{2})\.[[:space:]].*/\1/p" It prints 12 as expected, since \1 refers to the first capturing group. However, if \0 is used instead of \1, the output is 12. Chapter Name, the…
Peppershaker
  • 111
  • 1
  • 8
4
votes
4 answers

Error in tag separated by `|` using Regex python

I want to add | before every tag. Please check the below code that I have used. tags = ['XYZ', 'CREF', 'BREF', 'RREF', 'REF'] string_data = 'XYZ:MUMBAI UNIVERSITYCREF:PUNE UNIVERSITYBREF:DADAR UNIVERSITYRREF:KOLHAPUR UNIVERCITY LLCREF:SOLAPUR…
Akshay Godase
  • 187
  • 1
  • 12
4
votes
1 answer

Sed - How to Print Regex Groups in Multi-Line?

Input file (test): 123456This is link1789This is link20123 Desired output: link1 link2 What I have done: $ sed -e '//!{N;b begin};s//QQ/;/
hahakubile
  • 6,978
  • 4
  • 28
  • 18
4
votes
5 answers

Referencing nested groups in JavaScript using string replace using regex

Because of the way that jQuery deals with script tags, I've found it necessary to do some HTML manipulation using regular expressions (yes, I know... not the ideal tool for the job). Unfortunately, it seems like my understanding of how captured…
Jacob
  • 77,566
  • 24
  • 149
  • 228
4
votes
1 answer

Perl RegEx non-capturing group with alternative capturing within the group

I'm trying to parse out some mail logs that have the three following possible formats for the relay. Oct 24 03:49:10 mxout/mxout/1.1.1.1 sendmail[4642]: x9NA4Wbp011336: to=, delay=1+00:44:37, xdelay=00:00:00, mailer=esmtp,…
tleif
  • 95
  • 5
4
votes
2 answers

Using Positive LookAhead and LookBehind To Mask Password In Database Connection String

I have an exception handler method in my VB.NET application that retrieves details from the last exception to occur and e-mails that information to our Help Desk (me) for diagnostics and troubleshooting. One instance of a possible exception is when…
G_Hosa_Phat
  • 976
  • 2
  • 18
  • 38
4
votes
3 answers

Get Line number of Text.RegularExpressions.Regex matches

I use PowerShell to parse a directory of Log files and extract all XML entries out of the log files. This works pretty ok. However since a log file can contain many of these xml bits and pieces I want to put the line number of the specific match it…
edelwater
  • 2,650
  • 8
  • 39
  • 67
4
votes
0 answers

Regex named capturing groups throw IllegalStateException in Scala

Edit 1: @GoodDok I can't use val regex = """(\d+) (\S+)""".r("Id", "Name") because regex pattern and matched string are supplied by user. Number of groups and naming is custom every time. Edit 2: @Wiktor Stribiżew The question is different than…
4
votes
2 answers

javascript regular expressions - groups

I"m currently studying regular expression groups. I'm having trouble fully understanding the first example presented in the book under groups. The book gives the following example: /(\S+) (\S*) ?\b(\S+)/ I understand that this will match at most…
4
votes
2 answers

RegEx capturing group in Elixir

I want to know how this Elixir regex work. Regex.run(~r{(*UTF)([^\w])+}, "dd!!%%%") when I execute this regex, the output is ["!!%%%", "%"] I'm not able to understand why the last % is repeated after matching the regex.
edwinallenz
  • 340
  • 3
  • 12
4
votes
4 answers

RegEx for matching a word after specific word in multiple lines

There is regex feature to find words instead of "Ctrl + F" in some editor like VS Code, I'm trying to find a word after a specific word with some another lines. For example, how to use regex to filter those "someFunction" with the specific…
4
votes
1 answer

RegEx for matching words only formed with a list of letters

Given a set of words, I need to know which words are formed only by a set of letters. This word can not have more letters than allowed, even if this letter is part of the verification set. Example: Char set: a, a, ã, c, e, l, m, m, m, o, o, o, o, t…