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
2404
votes
18 answers

What is a non-capturing group in regular expressions?

How are non-capturing groups, i.e., (?:), used in regular expressions and what are they good for?
never_had_a_name
  • 90,630
  • 105
  • 267
  • 383
316
votes
3 answers

Named regular expression group "(?Pregexp)": what does "P" stand for?

In Python, the (?P…) syntax allows one to refer to the matched string through its name: >>> import re >>> match = re.search('(?P.*) (?P.*)', 'John 123456') >>> match.group('name') 'John' What does "P" stand for? I could…
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
286
votes
1 answer

python re.sub group: number after \number

How can I replace foobar with foo123bar? This doesn't work: >>> re.sub(r'(foo)', r'\1123', 'foobar') 'J3bar' This works: >>> re.sub(r'(foo)', r'\1hi', 'foobar') 'foohibar' I think it's a common issue when having something like \number. Can anyone…
zhigang
  • 6,597
  • 4
  • 30
  • 24
239
votes
33 answers

RegEx for matching UK Postcodes

I'm after a regex that will validate a full complex UK postcode only within an input string. All of the uncommon postcode forms must be covered as well as the usual. For instance: Matches CW3 9SS SE5 0EG SE50EG se5 0eg WC2H 7LT No Match aWC2H…
Kieran Benton
  • 8,739
  • 12
  • 53
  • 77
238
votes
19 answers

RegEx to extract all matches from string using RegExp.exec

I'm trying to parse the following kind of string: [key:"val" key2:"val2"] where there are arbitrary key:"val" pairs inside. I want to grab the key name and the value. For those curious I'm trying to parse the database format of task warrior. Here…
gatlin
  • 2,399
  • 2
  • 14
  • 6
191
votes
5 answers

Vim Regex Capture Groups [bau -> byau : ceu -> cyeu]

I have a list of words: bau ceu diu fou gau I want to turn that list into: byau cyeu dyiu fyou gyau I unsuccessfully tried the command: :%s/(\w)(\w\w)/\1y\2/g Given that this doesn't work, what do I have to change to make the regex capture groups…
Christian
  • 25,249
  • 40
  • 134
  • 225
179
votes
2 answers

Python Regex instantly replace groups

Is there any way to directly replace all groups using regex syntax? The normal way: re.match(r"(?:aaa)(_bbb)", string1).group(1) But I want to achieve something like this: re.match(r"(\d.*?)\s(\d.*?)", "(CALL_GROUP_1) (CALL_GROUP_2)") I want to…
user1467267
161
votes
4 answers

Can I use an OR in regex without capturing what's enclosed?

I'm using rubular.com to build my regex, and their documentation describes the following: (...) Capture everything enclosed (a|b) a or b How can I use an OR expression without capturing what's in it? For example, say I want to capture either…
goggin13
  • 7,876
  • 7
  • 29
  • 44
131
votes
7 answers

Can I replace groups in Java regex?

I have this code, and I want to know, if I can replace only groups (not all pattern) in Java regex. Code: //... Pattern p = Pattern.compile("(\\d).*(\\d)"); String input = "6 example input 4"; Matcher m = p.matcher(input); if…
wokena
  • 1,301
  • 4
  • 15
  • 17
118
votes
2 answers

Regex optional group

I am using this regex: ((?:[a-z][a-z]+))_(\d+)_((?:[a-z][a-z]+)\d+)_(\d{13}) to match strings like this: SH_6208069141055_BC000388_20110412101855 separating into 4 groups: SH 6208069141055 BC000388 20110412101855 Question: How do I make the…
joe
  • 8,344
  • 9
  • 54
  • 80
78
votes
1 answer

Guide on how to use regex in Nginx location block section?

Nginx regex location syntaxe Regex expressions can be used with Nginx location block section, this is implemented with the PCRE engine. What does exactly this feature support as it is not fully documented?
intika
  • 8,448
  • 5
  • 36
  • 55
56
votes
6 answers

Negating a backreference in Regular Expressions

if a string has this predicted format: value = "hello and good morning" Where the " (quotations) might also be ' (single quote), and the closing char (' or ") will be the same as the opening one. I want to match the string between the quotation…
Yuval A.
  • 5,849
  • 11
  • 51
  • 63
46
votes
5 answers

extract substring using regex in groovy

If I have the following pattern in some text: def articleContent = "" I would like to extract the "Hellow World" part, so I use the following code to match it: def contentRegex = "" def contentMatcher…
RicardoE
  • 1,665
  • 6
  • 24
  • 42
44
votes
4 answers

preg_replace how to replace only matching xxx($1)yyy pattern inside the selector

I'm trying to use a regular expression to erase only the matching part of an string. I'm using the preg_replace function and have tried to delete the matching text by putting parentheses around the matching portion.…
PartySoft
  • 2,749
  • 7
  • 39
  • 55
39
votes
3 answers

Regular Expression in sed for multiple replacements in one statement

I want to sanitise some input and replace several characters with acceptable input, e.g. a Danish 'å' with 'aa'. This is easily done using several statements, e.g. /æ/ae/, /å/aa/, /ø/oe/, but due to tool limitations, I want to be able to do this in…
Jan
  • 853
  • 2
  • 9
  • 18
1
2 3
99 100