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
0
votes
1 answer

How to get text that is before and after of a matched group in a regex expression

I have following regex that matches any number in the string and returns it in the group. ^.*[^0-9]([0-9]+).*$  $1 Is there a way I can get the text before and after of the matched group i.e. also as my endgoal is to reconstruct the string by…
Maven
  • 14,587
  • 42
  • 113
  • 174
0
votes
1 answer

Using regex capturing groups to dynamically return the group that matches the criteria

I have URL string such as /questions/12314454/syntax-error. I use the following regex with capturing groups in a nginx map to get a number and replace it with a static string i.e. ID. "~(.*/)([0-9]{5,})(/.*)$" $1ID$3; However, above only works when…
Maven
  • 14,587
  • 42
  • 113
  • 174
0
votes
1 answer

How to put '0' in front a day number only if it is one number and not two with this regexs?

Here the input examples: import re input_text = "Serian 4 unidades de mermelada para el dia 04 del 8 de este año 2023" #example 1 input_text = "Hay 10 unidades para el dia 15 del 12 y seran 9 ya para el 7 de noviembre" #example 2 input_text =…
Matt095
  • 857
  • 3
  • 9
0
votes
2 answers

How to extract time data from a string using regex.group()?

I'm supposed to normalize time statements in input, converting them to a standard format. The input statements contain an hour, possibly minutes, and a part of day (morning or evening). The part of day can be expressed multiple ways. The hour might…
Matt095
  • 857
  • 3
  • 9
0
votes
0 answers

Ignore multiple entries of word boundary inside capturing group of + Quantifier

I have this regex /{([\d,-]+)}/ which is used to highlight lines in markdown and allows input in this format - {2,3-6,8}. Now, I wanted to include showLineNumbers prop in it too such as {2,3-6,8,showLineNumbers} and couldn't figure regex for this.…
Gurkiran Singh
  • 189
  • 2
  • 11
0
votes
2 answers

How to extract the password from this string in bash: "--db_host db --db_password $£é5+dd/gcç# --db_name zorgl"?

I have tried this on Debian, but it fails: echo "--db_host db --db_password $£é5+dd/gcç# --db_name zorgl" | sed -e "s/^.*db_password *\([^ ]+\) +.*$/\1/" Result (no match): --db_host db --db_password $£é5+dd/gcç# --db_name zorgl Expected…
lalebarde
  • 1,684
  • 1
  • 21
  • 36
0
votes
2 answers

Regex matching mixed string segments containing operator, string designator, and curly-brace group

I am looking for a C# regex solution to match/capture some small but complex chunks of data. I have thousands of unstructured chunks of data in my database (comes from a third-party data store) that look similar to this: not BATTCOMPAR{275} and…
HerrimanCoder
  • 6,835
  • 24
  • 78
  • 158
0
votes
1 answer

How to use REGEX to extract ASIN from different types of Amazon URL

I have this these kinds of Amazon URLs and I want to extract ASIN from…
Smith O.
  • 217
  • 4
  • 16
0
votes
0 answers

How to deal with backslash concatenation in RegExp breaking expressions, e.g. those that contain capturing groups brackets?

Creating RegEx and combining it with variables that are strings with a certain pattern doesn't always work out well, if the pattern doesn't change to work with the string. "\\" + ) = \) in RegEx. Which is an escaped ) and not the closing bracket of…
karl-police
  • 983
  • 8
  • 21
0
votes
2 answers

How to delete all other characters except match case using Regex

arn:aws:iam::aws:policy/AmazonEC2FullAccess arn:aws:iam::aws:policy/IAMFullAccess arn:aws:iam::s:policy/CloudWatchAgentServerPolicy arn:aws:iam::aws:policy/AdministratorAccess arn:aws:iam::aws:policy/aws-service-role/AWSSupportServiceRolePolicy arn:a…
0
votes
2 answers

How can I get the specific part from the link using regular expressions (regex),,,

The format of Url is - https://example.com/items/html5-templates/654321 Firstly I want to take the link without last numbers. e.g: https://example.com/items/html5-templates/ And, Secondly, want to take only the last part (numbers) from the…
0
votes
0 answers

Regex special sequence

I altered a code from solo learn app but got confused : import re pattern = r'(.+)(.+) \2' match = re.match(pattern , 'ABC bca cab ABC') if match: print('Match 1' , match.group()) match = re.match(pattern , 'abc BCA cab BCA') if match: …
0
votes
1 answer

Java Regex to validate group field pattern example - abc.def.gh1

I am just writing some piece of java code where I need to validate groupId (maven) passed by user. For example - com.fb.test1. I have written regex which says string should not start and end with '.' and can have alphanumeric characters delimited by…
priyam singh
  • 105
  • 3
  • 16
0
votes
2 answers

Regex : referencing an already captured group

I am capturing Bar via a named groupA and I would like to re-reference groupA capture to get a second group. Sample string : Foo:Bar Lorem ipsum Bar-564875XC4 dolor sit amet Regex to capture Bar in groupA : Foo:(?[^\s]+) Question : how to I…
Philippe
  • 103
  • 1
  • 1
  • 8
0
votes
2 answers

replace all special characters expect first occurrence of +

I want to replace everything other than numeric char from a string, but if + appears at the starting of the string it should be ignored. for example 1234++!@#$%^&*()_+=-;',.><:" becomes 1234 +1234 becomes +1234 ++++1234 becomes…
Neeraj Pathak
  • 311
  • 2
  • 18