Questions tagged [capture-group]

Question topic: regular expression capture group. A regular expression capture group is a portion of a regular expression enclosed in parentheses, which can be used to match a specific set of characters.

Overview

A regular expression capture group is a portion of a regular expression enclosed in parentheses, which can be used to match a specific set of characters.

The matched characters can be accessed later in the program, for example to extract a specific piece of information from a string or to replace the matched characters with new text.

Capture groups are numbered starting from 1, and can be accessed in the program using the group() or groups() methods of a match object.

Capture groups are a special case of non-capturing groups, where grouping is needed, but not separate capturing.

185 questions
171
votes
17 answers

Regular expression for duplicate words

I'm a regular expression newbie and I can't quite figure out how to write a single regular expression that would "match" any duplicate consecutive words such as: Paris in the the spring. Not that that is related. Why are you laughing? Are my my…
Joshua
  • 6,643
  • 15
  • 55
  • 76
118
votes
8 answers

How to get capturing group functionality in Go regular expressions

I'm porting a library from Ruby to Go, and have just discovered that regular expressions in Ruby are not compatible with Go (google RE2). It's come to my attention that Ruby & Java (plus other languages use PCRE regular expressions (perl compatible,…
Plastikfan
  • 3,674
  • 7
  • 39
  • 54
107
votes
9 answers

Regex group capture in R with multiple capture-groups

In R, is it possible to extract group capture from a regular expression match? As far as I can tell, none of grep, grepl, regexpr, gregexpr, sub, or gsub return the group captures. I need to extract key-value pairs from strings that are encoded…
Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89
46
votes
4 answers

Capture groups not working in NSRegularExpression

Why is this code only spitting out the entire regex match instead of the capture group? Input @"A long string containing Name:A name here amongst other things" Output expected A name here Actual output Name:A name…
Maciej Swic
  • 11,139
  • 8
  • 52
  • 68
34
votes
4 answers

Why won't re.groups() give me anything for my one correctly-matched group?

When I run this code: print re.search(r'1', '1').groups() I get a result of (). However, .group(0) gives me the match. Shouldn't groups() give me something containing the match?
dtc
  • 1,774
  • 2
  • 21
  • 44
21
votes
1 answer

C++11 Regex Capture Groups By Name

I am converting my boost-based regular expressions to C++11 regex. I have a capture group called url: \s*?=\s*?(("(?.*?)")|('?.*?)')) With boost, if you had an smatch you could call match.str("url") to get the capture group by name. With…
Travis Parks
  • 8,435
  • 12
  • 52
  • 85
19
votes
2 answers

Regex - Repeating Capturing Group

I'm trying to figure out how I can repeat a capture group on the comma-separated values in this the following url string: id=1,2;name=user1,user2,user3;city=Oakland,San Francisco,Seattle;zip=94553,94523; I'm using this RegExp which is return results…
Jordan Davis
  • 1,485
  • 7
  • 21
  • 40
13
votes
5 answers

Replace specific capture group instead of entire regex in Perl

I've got a regular expression with capture groups that matches what I want in a broader context. I then take capture group $1 and use it for my needs. That's easy. But how to use capture groups with s/// when I just want to replace the content of…
flohei
  • 5,248
  • 10
  • 36
  • 61
10
votes
1 answer

Regex: capturing groups within capture groups

Intro (you can skip to What if... if you get bored with intros) This question is not directed to VBScript particularly (I just used it in this case): I want to find a solution for general regular expressions usage (editors included). This started…
Armfoot
  • 4,663
  • 5
  • 45
  • 60
9
votes
2 answers

Are non-capturing groups redundant?

Are optional non-capturing groups redundant? Is the following regex: (?:wo)?men semantically equivalent to the following regex? (wo)?men
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
7
votes
1 answer

Sed append regex capture groups

If someone can please assist, I'm trying to do a sed append using regex and capture groups but its not working fully: echo "#baseurl=http://mirror.centos.org/centos/$releasever/contrib/$basearch/" | sed -re…
Jahed Uddin
  • 301
  • 1
  • 10
6
votes
3 answers

Problem with whitespace in a RegEx with capture groups

I've got a regular expression that I'm trying to match against the following types of data, with each token separated by an unknown number of spaces. Update: "Text" can be almost any character, which is why I had .* initially. Importantly, it can…
Dov
  • 15,530
  • 13
  • 76
  • 177
6
votes
1 answer

How to capture 'multiple' repeated groups with Regular Expressions

I have the following text file I would like to parse out to get the individual fields: host_group_web = ( ) host_group_lbnorth = ( lba050 lbhou002 lblon003 ) The fields that I would like to extract are in bold host_group_web = (…
mindrunner
  • 166
  • 3
  • 11
6
votes
2 answers

Is there an equivalent of "&" in R's regular expressions for backreference to entire match?

When I use vim, I often use & to backreference the entire match within substitutions. For example, the following replaces all instances of "foo" with "foobar": %s/foo/&bar/g The benefit here is laziness: I don't have to type the parenthesis in the…
crazybilly
  • 2,992
  • 1
  • 16
  • 42
5
votes
2 answers

Perl: named capturing group defaults at replacement string

The following Perl code fails on the second attempt of dereferencing the capturing group "ext" use strict; use warnings; foreach(qw/2.1 2/){ #foreach(@ARGV){ my $out= sprintf("%.2f", $_); $out =~…
mcaustria
  • 123
  • 6
1
2 3
12 13