Questions tagged [capture-group]

157 questions
134
votes
13 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,123
  • 15
  • 52
  • 76
96
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,525
  • 12
  • 67
  • 88
70
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
  • 2,722
  • 5
  • 32
  • 47
43
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
  • 10,562
  • 8
  • 46
  • 64
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? Update: Thanks for the answers. So that means if I do re.search()…
dtc
  • 1,608
  • 2
  • 20
  • 40
16
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
  • 7,747
  • 9
  • 44
  • 83
14
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,197
  • 4
  • 16
  • 36
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,137
  • 2
  • 39
  • 58
10
votes
4 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 do use capture groups with s/// when I just want to replace the content of…
flohei
  • 5,116
  • 10
  • 34
  • 59
8
votes
1 answer

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
  • 237,063
  • 85
  • 359
  • 638
6
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
  • 171
  • 10
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,652
  • 13
  • 38
5
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
  • 14,039
  • 12
  • 71
  • 151
4
votes
2 answers

PowerShell RegEx to split MAC address

I need to verify MAC address in RAW format using RegEx and split it into an array of 6 values by 2 characters. When I use following pattern, I get content of last iteration of capture group only: PS C:\Windows\System32> "708BCDBC8A0D" -match…
rga.cz
  • 53
  • 3
4
votes
2 answers

Identifying capture groups in a Regex Pattern

Is there a way in Java (perhaps with an additional Open Source library) to identify the capture groups in a java.util.regex.Pattern (i.e. before creating a Matcher) Example from the Java docs: Capturing groups are numbered by counting their…
peter.murray.rust
  • 35,191
  • 41
  • 141
  • 211
1
2 3
10 11