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

Nginx location's regex, is escaping the forward slash needed?

Nginx uses the PCRE engine for evaluating regular expressions, the documentation state that / delimiter is not used so we don’t have to escape the forward slash / in an URI as we may do in a standard regex. An example of a valid nginx regex is…
intika
  • 8,448
  • 5
  • 36
  • 55
10
votes
4 answers

Is it possible to erase a capture group that has already matched, making it non-participating?

In PCRE2 or any other regex engine supporting forward backreferences, is it possible to change a capture group that matched in a previous iteration of a loop into a non-participating capture group (also known as an unset capture group or…
Deadcode
  • 860
  • 9
  • 15
10
votes
1 answer

functional difference between lookarounds and non-capture group?

I'm trying to come up with an example where positive look-around works but non-capture groups won't work, to further understand their usages. The examples I"m coming up with all work with non-capture groups as well, so I feel like I"m not fully…
Moondra
  • 4,399
  • 9
  • 46
  • 104
10
votes
4 answers

Powershell - Regular Expression Multiple Matches

Maybe my reasoning is faulty, but I can't get this working. Here's my regex: (Device\s#\d(\n.*)*?(?=\n\s*Device\s#|\Z)) Try it: http://regex101.com/r/jQ6uC8/6 $getdevice is the input string. I'm getting this string from the Stream/Output from a…
Frankstar
  • 306
  • 1
  • 3
  • 10
9
votes
2 answers

Firefox gives SyntaxError: invalid regexp group

I have few regular expression used for form validation and I noticed that my project is not accessible through firefox as it shows nothing! but give the error in the console, SyntaxError: invalid regexp group nicRegex is checking for National…
Hasini Silva
  • 344
  • 1
  • 6
  • 18
9
votes
2 answers

using regex g flag in java

Is it possible to use regex global g flag in java pattern ? I tried with final Pattern pattern = Pattern.compile(regex,Pattern.DOTALL); but it do not behave like global flag. Do we have any workaround for it in java ? My Regex is : private final…
Sandeep Chauhan
  • 141
  • 1
  • 2
  • 8
9
votes
2 answers

Groovy: Idiomatic way to replace captured groups

I've a string like this: docker login -u username -p password docker-registry-url. I execute the command in a Groovy script with execute. For debugging purposes, I print the command before execution, but since it contains sensitive data, I obfuscate…
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
8
votes
1 answer

Raku regex: How to use capturing group inside lookaheads

How can I use capturing groups inside lookahead assertion? This code: say "ab" ~~ m/(a) /; returns: 「a」 0 => 「a」 But I was expecting to also capture 'b'. Is there a way to do so? I don't want to leave 'b' outside of the lookahead…
Julio
  • 5,208
  • 1
  • 13
  • 42
8
votes
1 answer

Regex: Capture one or more groups if exists (Java)

I want to capture groups matching a pattern where the input can contain this group one or more times. Example: input = 12361 randomstuff371 12 Mar 16 138more random381 stuff73f I want to capture the "12 Mar 16". From this I have easily used the…
J Z
  • 89
  • 1
  • 8
8
votes
2 answers

android java regex named groups

I would like to use matcher.group("login"); In android 8+ on eclipse, but no Matcher.group(String) seems to exist. Have you got a (built in) solution ?
hanoo
  • 4,175
  • 2
  • 26
  • 19
7
votes
3 answers

Negative lookbehind on a capture group

I'm trying to write some regex that will allow me to do a negative lookbehind on a capture group so that I can extract possible references from emails. I need to know how to look behind from a certain point to the first white space. If a digit is…
Lank
  • 105
  • 1
  • 5
7
votes
1 answer

Regex named groups in R

For all intents and purposes, I am a Python user and use the Pandas library on a daily basis. The named capture groups in regex is extremely useful. So, for example, it is relatively trivial to extract occurrences of specific words or phrases and to…
user1718097
  • 4,090
  • 11
  • 48
  • 63
7
votes
1 answer

How golang replace string by regex group?

I want to use regex group to replace string in golang, just like as follow in python: re.sub(r"(\d.*?)[a-z]+(\d.*?)", r"\1 \2", "123abc123") # python code So how do I implement this in golang?
roger
  • 9,063
  • 20
  • 72
  • 119
7
votes
4 answers

Java Matcher groups: Understanding The difference between "(?:X|Y)" and "(?:X)|(?:Y)"

Can anyone explain: Why the two patterns used below give different results? (answered below) Why the 2nd example gives a group count of 1 but says the start and end of group 1 is -1? public void testGroups() throws Exception { String…
user358795
  • 145
  • 1
  • 7
7
votes
2 answers

Powershell: Replacing regex named groups with variables

Say I have a regular expression like the following, but I loaded it from a file into a variable $regex, and so have no idea at design time what its contents are, but at runtime I can discover that it includes the "version1", "version2", "version3"…
Hoobajoob
  • 2,748
  • 3
  • 28
  • 33