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:

191 questions
1994
votes
16 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
  • 80,383
  • 96
  • 257
  • 374
146
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
  • 21,975
  • 33
  • 117
  • 195
103
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
  • 348,637
  • 121
  • 546
  • 611
88
votes
4 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
  • 8,203
  • 11
  • 32
  • 45
77
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
  • 34,978
  • 62
  • 174
  • 253
76
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
  • 85,654
  • 109
  • 315
  • 497
43
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
  • 348,637
  • 121
  • 546
  • 611
38
votes
7 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
  • 2,899
  • 4
  • 19
  • 24
37
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
  • 737
  • 1
  • 5
  • 12
23
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
  • 353
  • 1
  • 3
  • 7
17
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
  • 16,976
  • 14
  • 66
  • 113
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
  • 33,587
  • 8
  • 84
  • 70
10
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…
8
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
  • 31,183
  • 9
  • 64
  • 147
6
votes
1 answer

SQL find-and-replace regular-expression capturing-group limit?

I need to convert data from a spreadsheet into insert statements in SQL. I've worked out most of the regular expressions for using the find and replace tool in SSMS, but I'm running into an issue when trying to reference the 9th parenthesized item…
1
2 3
12 13