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
38
votes
4 answers

How do I group regular expressions past the 9th backreference?

Ok so I am trying to group past the 9th backreference in notepad++. The wiki says that I can use group naming to go past the 9th reference. However, I can't seem to get the syntax right to do the match. I am starting off with just two groups to make…
Steven Combs
  • 1,890
  • 6
  • 29
  • 54
35
votes
8 answers

How do I write a regular expression that excludes rather than matches, e.g., not (this|string)?

I am stumped trying to create an Emacs regular-expression that excludes groups. [^] excludes individual characters in a set, but I want to exclude specific sequences of characters: something like [^(not|this)], so that strings containing "not" or…
Anycorn
  • 50,217
  • 42
  • 167
  • 261
30
votes
2 answers

Match list of words without the list of chars around

I have this regex (?:$|^| )(one|common|word|or|another)(?:$|^| ) which matches fine unless the two words are next to each other. One one's more word'word common word or another word more another More and more years to match one or more other…
San
  • 3,933
  • 7
  • 34
  • 43
26
votes
2 answers

Regex and the OR operator without grouping in Python?

Here are the cases. I'm looking for the following pattern in a log file. All strings are in the form of AB_N or CDE_N. AB and CDE are fixed letters, followed by an underscore. N can be either 2 or 3 numbers. I tried (AB|CDE)_\d{2,3} but that…
pedram
  • 2,931
  • 3
  • 27
  • 43
24
votes
6 answers

Kotlin Regex named groups support

Does Kotlin have support for named regex groups? Named regex group looks like this: (?...)
gs_vlad
  • 1,409
  • 4
  • 15
  • 29
24
votes
3 answers

scala regex group matching and replace

val REGEX_OPEN_CURLY_BRACE = """\{""".r val REGEX_CLOSED_CURLY_BRACE = """\}""".r val REGEX_INLINE_DOUBLE_QUOTES = """\\\"""".r val REGEX_NEW_LINE = """\\\n""".r // Replacing { with '{' and } with '}' str = REGEX_OPEN_CURLY_BRACE.replaceAllIn(str,…
yalkris
  • 2,596
  • 5
  • 31
  • 51
23
votes
3 answers

C++11 regex: digit after capturing group in replacement string

My regex_replace expression uses group $1 right before a '0' character in the replacement string like so: #include #include #include using namespace std; int main() { regex regex_a( "(.*)bar(.*)" ); cout <<…
srking
  • 4,512
  • 1
  • 30
  • 46
21
votes
6 answers

How can I store captures from a Perl regular expression into separate variables?

I have a regex: /abc(def)ghi(jkl)mno(pqr)/igs How would I capture the results of each parentheses into 3 different variables, one for each parentheses? Right now I using one array to capture all the results, they come out sequential but then I have…
Incognito
  • 1,883
  • 5
  • 21
  • 28
15
votes
4 answers

vim yank all matches of regex group into register

I know that I can yank all matched lines into register A like this: :g/regex/y/A But I can't seem to figure out how to yank match regex groups into register A: :g/\(regex\)/\1y A (E10: \ should be followed by /, ? or &)
user2506293
  • 805
  • 1
  • 7
  • 13
13
votes
1 answer

Notepad++ regex replace named groups

I try replace author Full name with article title I have a list of articles, similar like this: Albershein P., Nevis D. J. A method for analysis of sugars in plant cell wall polysaccharides by gasliquid chromatography // J. Carbohydrate Research.…
Victor Dronov
  • 139
  • 1
  • 12
12
votes
2 answers

python regex: duplicate names in named groups

Is there a way to use same name in regex named group in python? e.g.(?Pfoo)|(?Pbar). Use case: I am trying to capture type and id with this regex: /(?=videos)((?Pvideos)/(?P\d+))|(?P\w+)/?(?Pv)?/?(?P\d+)? from this…
tsh
  • 877
  • 8
  • 12
12
votes
1 answer

What does ?: in a regular expression mean?

Please explain the meaning of this regular expression and what groups the expression will generate? $string =~ m/^(\d*)(?: \D.*?)(\d*)$/ PS: I'm re-factoring Perl code to Java.
Saravanan
  • 149
  • 1
  • 1
  • 6
11
votes
3 answers

how to interpolate string containing capture-group parentheses as regex in Raku?

I want to match against a programmatically-constructed regex, containing a number of (.*) capture groups. I have this regex as a string, say my $rx = "(.*)a(.*)b(.*)" I would like to interpolate that string as a regex and match for it. The docs…
grobber
  • 1,083
  • 1
  • 9
  • 20
11
votes
7 answers

Javascript RegExp non-capturing groups

I am writing a set of RegExps to translate a CSS selector into arrays of ids and classes. For example, I would like '#foo#bar' to return ['foo', 'bar']. I have been trying to achieve this with "#foo#bar".match(/((?:#)[a-zA-Z0-9\-_]*)/g) but it…
Alexandre Kirszenberg
  • 35,938
  • 10
  • 88
  • 72
10
votes
2 answers

Raku regex: How to know which group was captured at an alternation

With perl (and almost any regex flavour), every group is numbered sequentially. So for example, this code: 'bar' =~ m/(foo)|(bar)/; print $1 // 'x'; # (1-based index) print $2 // 'x'; # (1-based index) prints xbar However, with Raku it behaves…
Julio
  • 5,208
  • 1
  • 13
  • 42