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
3
votes
2 answers

sed - print translated HEX using capture group

I would like to print directly with sed a HEX value translation by isolating the HEX values in capture groups. This works: echo bbb3Accc | sed -n 's/3A/\x3A/p' bbb:ccc ...but this doesn't work: echo bbb3Accc | sed 's/\(3A\)/\x\1/' bbbx3Accc ...or…
one-liner
  • 791
  • 1
  • 9
  • 19
3
votes
1 answer

What is the Julia syntax for regex capture groups (e.g. $1 in Perl)?

I wonder what is the Julia's equivalent of $1, $2, ... in Perl's regex (accessing substrings captured by regex)? For instance $s = "some random string"; $s =~ m/(o.e).+(i.g)/; print $1, "\n", $2; prints ome ing
LifeWorks
  • 396
  • 1
  • 4
  • 12
3
votes
1 answer

How can I do multiple replace using a shared backreference?

I have a need to do some data-transformation for data load compatibility. The nested key:value pairs need to be flattened and have their group id prepended to each piece of child data. I've been trying to understand the page at Repeating a…
rumpled
  • 43
  • 5
3
votes
2 answers

Named capture groups with grep

I use Unix grep. I would like to know how can I handle named capture groups with it. Currently this is what I have: echo "foobar" | grep -P "(?.)ooba(?.)" So in theory, I have q=f and w=r, however I don't know how can I use these variables or…
Letokteren
  • 749
  • 1
  • 15
  • 28
3
votes
1 answer

Regex URL Capturing Group

I'm writing a regex expression and trying to get each part of a URL into it's own capture group for extraction: Protocol (http,https) Sub Domain (sub) Domain (domain) Domain Extension (com,net) Path (/path/to/file - this is to be the path to the…
user7892649
3
votes
2 answers

java regex - capture repeated groups

I'm using Java's regex library. I want to validate a string against the following format: 31,5,46,7,86(...) The amount of numbers is not known. I want to make sure there is at least one number in that string, and that every two numbers are…
Roy Millo
  • 33
  • 1
  • 4
3
votes
3 answers

Python regular expression returning extra capture group for last character matched

I am trying to create a regular expression that will take strings and break them up into three groups: (1) Any one of a specific list of words at the beginning of a string. (2) Any one of specific list of words at the end of a string. (3) all of…
J. Taylor
  • 4,567
  • 3
  • 35
  • 55
3
votes
4 answers

extract last match from string in c#

i have strings in the form [abc].[some other string].[can.also.contain.periods].[our match] i now want to match the string "our match" (i.e. without the brackets), so i played around with lookarounds and whatnot. i now get the correct match, but i…
knittl
  • 246,190
  • 53
  • 318
  • 364
3
votes
2 answers

Get $1 capture group with dynamic RegExp

I was curious if anyone knows what I'm doing wrong here. I want to use a variable inside the expression but for some reason when I try to, it doesn't seem to grab the search term with the $1. This returns correctly: $('.content').html(function(_,i)…
daless14
  • 67
  • 5
3
votes
2 answers

Why doesn't substitution in list context return a list of captures?

As per perlop, in list context, a matching operator m// with a regex that has a capture group and a /g modifier will return a list of captures: my $str = "8a9b0c"; my @res = ($str =~ m/(\d)/g); print @res; # Prints "8 9 0" However, I can't find in…
DVK
  • 126,886
  • 32
  • 213
  • 327
3
votes
1 answer

how to store a regex-capture-group as a variable in vim script?

i'm trying to write a vimscript to refactor some legacy code. roughly i have a lot of files in this format $this['foo'] = array(); { $this['foo']['id'] = 123; $this['foo']['name'] = 'name here'; $this['foo']['name2'] = 'name here2'; …
David Chan
  • 7,347
  • 1
  • 28
  • 49
3
votes
2 answers

What is the purpose of non-capture groups

I was reading the Groovy tutorial and they talk about how you can create non-capturing groups by leading the group off with ?:. This way the group will not come up on the matcher. What I don't understand is why you would want to explicitly say do…
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
3
votes
1 answer

Accessing my capture group in regex pattern matching in Expect to a variable

I can't get this to work and am not sure why. I have a line that has spaces between numbers at the end of the line. I need Expect to match this line and put the numbers and spaces into a capture group and I need to access that capture group later…
harperville
  • 6,921
  • 8
  • 28
  • 36
3
votes
3 answers

C# Regex: How Can I Replace Tokens With Strings Generated at Run-time?

Given the following input and regex strings: const string inputString = "${Principal}*${Rate}*${Years}"; const string tokenMatchRegexString = @"\${([^}]+)}"; How can I replace each token (i.e. ${Principal}, ${Rate}, and ${Years}) with the return…
Jim G.
  • 15,141
  • 22
  • 103
  • 166
2
votes
2 answers

Replace the string after second forward slash using a RegEx

Having a string (URL path) as for example: /this/is/some/url I would like to remove the first occurrence of / and change the string after the second forward slash (in the example some) by another string let's say is. The result should be…
ReynierPM
  • 17,594
  • 53
  • 193
  • 363
1 2
3
12 13