Questions tagged [capturing-group]

Capturing groups are regular expression constructs that makes use of capturing in regex to capture parts of the matched string during a match sequence in the regexp pattern.

Capturing groups and s are regular expression constructs that makes use of capturing in to perform matching and replacement that remembers parts of the matched string during a match sequence in the regexp pattern.

Read more:

223 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
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
108
votes
3 answers

How can we match a^n b^n?

This is the second part of a series of educational regex articles. It shows how lookaheads and nested references can be used to match the non-regular languge anbn. Nested references are first introduced in: How does this regex find triangular…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
97
votes
5 answers

How to capture an arbitrary number of groups in JavaScript Regexp?

I would expect this line of JavaScript: "foo bar baz".match(/^(\s*\w+)+$/) to return something like: ["foo bar baz", "foo", " bar", " baz"] but instead it returns only the last captured match: ["foo bar baz", " baz"] Is there a way to get all the…
disc0dancer
  • 9,185
  • 11
  • 36
  • 46
88
votes
5 answers

What does this Django regular expression mean? `?P`

I have the following regular expression (regex) in my urls.py and I'd like to know what it means. Specifically the (?P portion of the regex. r'^category/(?P[-\w]+)/$
locoboy
  • 38,002
  • 70
  • 184
  • 260
88
votes
5 answers

Scala capture group using regex

Let's say I have this code: val string = "one493two483three" val pattern = """two(\d+)three""".r pattern.findAllIn(string).foreach(println) I expected findAllIn to only return 483, but instead, it returned two483three. I know I could use unapply to…
Geo
  • 93,257
  • 117
  • 344
  • 520
49
votes
8 answers

Get index of each capture in a JavaScript regex

I want to match a regex like /(a).(b)(c.)d/ with "aabccde", and get the following information back: "a" at index = 0 "b" at index = 2 "cc" at index = 3 How can I do this? String.match returns list of matches and index of the start of the complete…
user1527166
  • 3,229
  • 5
  • 26
  • 26
48
votes
4 answers

Regex optional capturing group?

After hours of searching I decided to ask this question. Why doesn't this regular expression ^(dog).+?(cat)? work as I think it should work (i.e. capture the first dog and cat if there is any)? What am I missing here? dog, cat dog, dog, cat dog,…
forsajt
  • 857
  • 1
  • 7
  • 13
44
votes
1 answer

How does this regex find triangular numbers?

Part of a series of educational regex articles, this is a gentle introduction to the concept of nested references. The first few triangular numbers are: 1 = 1 3 = 1 + 2 6 = 1 + 2 + 3 10 = 1 + 2 + 3 + 4 15 = 1 + 2 + 3 + 4 + 5 There are many…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
33
votes
4 answers

Why does re.sub replace the entire pattern, not just a capturing group within it?

re.sub('a(b)','d','abc') yields dc, not adc. Why does re.sub replace the entire capturing group, instead of just capturing group'(b)'?
Nick
  • 453
  • 1
  • 5
  • 7
24
votes
4 answers

sed - Get only the replaced string from a multiline input & omit unmatched lines!

I want sed to omit all non-matching lines, and only output the replaced string (of the single/multiple intended line/s). In other words: I've got a hay stack, and only want the needle returned, not all the hay which was searched and which remained…
porg
  • 1,079
  • 1
  • 11
  • 17
19
votes
2 answers

Scala regex Named Capturing Groups

In scala.util.matching.Regex trait MatchData I see that there support for groupnames , I thought that this was related to (Regex Named Capturing Groups) But since Java does not support groupnames until version 7 as I understand it (ref), Scala…
oluies
  • 17,694
  • 14
  • 74
  • 117
14
votes
1 answer

Negative lookahead with capturing groups

I'm attempting this challenge: https://regex.alf.nu/4 I want to match all strings that don't contain an ABBA pattern. Match: aesthophysiology amphimictical baruria calomorphic Don't…
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

Find and replace using regular expression, group capturing, and back referencing

I'm trying to perform a find and replace operation in SQL Server 2008 R2 Management Studio and employ a group capture so that I can back reference the groups in the replacement. I understand from this that SSMS uses the Visual Studio 2005 regex…
rory.ap
  • 34,009
  • 10
  • 83
  • 174
1
2 3
14 15