Questions tagged [capture-group]

157 questions
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
  • 119,765
  • 29
  • 201
  • 317
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
  • 5,730
  • 7
  • 23
  • 32
2
votes
1 answer

Multiple captures within a string

Haven't found a Q/A on SO that quite answers this situation. I have implemented solutions from some to get as far as I have. I'm parsing the header (metadata) part of VCF files. Each line has the format: ##TAG= I have a regex…
abalter
  • 7,589
  • 12
  • 75
  • 118
2
votes
2 answers

grep capture regex

I am trying to use grep to capture data below: "\\.xy$" "\\.ab$" "\\.ef\\.hi$" I have grep -Eo "((\\\\\.[a-zA-Z]+)){1,2}\\$" file two problems: It can capture things like \\.xy$, but not \\.xy\\.ef$ the returned results have literal $ at the…
user775187
  • 18,341
  • 7
  • 26
  • 35
2
votes
3 answers

How to match different groups in regex

I have the following string: "Josua de Grave* (1643-1712)" Everything before the * is the person's name, the first date 1634 is his birth date, 1712 is the date of his death. Following this logic I'd like to have 3 match groups for each one of the…
David Geismar
  • 2,327
  • 2
  • 28
  • 64
2
votes
0 answers

Named capture groups in the function version of `replace()`

I notice you can actually get passed your named capture groups in the replace callback: 'Yoda said "fear leads to anger."'.replace( /(?
\b\w+\b)(? leads to )(?\b\w+\b)/,
    (...args) => {
        let [{pre, betwixt, post}] =…
Hashbrown
  • 8,877
  • 7
  • 57
  • 71
2
votes
2 answers

Sort by named capture group within regular expression in powershell

I have a regular expression defined as follows: $regexpo = "^Amma\s(?'version'(\d+\.){3}\d)\.zip$" The above expression has label attached with a group - (\d+.){3}\d) And I am using it as follows: $Library = $wholeContent | Where-Object {…
sajis997
  • 687
  • 5
  • 21
2
votes
3 answers

JS split() Regex with Three Matches to a single Results Array

I'm trying to split a string of the type below in JS with split(). let shape = "Cube - Level 2: three-dimensional"; My desired end-state is something like: 0: "Cube" 1: "Level 2" 2: "three-dimensional" I can individually capture words preceding…
Marcatectura
  • 1,633
  • 5
  • 26
  • 46
2
votes
2 answers

Python : Convert Integers into a Count (i.e. 3 --> 1,2,3)

This might be more information than necessary to explain my question, but I am trying to combine 2 scripts (I wrote for other uses) together to do the following. TargetString (input_file) 4FOO 2BAR Result (output_file) 1FOO 2FOO 3FOO 4FOO 1BAR…
physlexic
  • 730
  • 1
  • 6
  • 18
2
votes
1 answer

How to highlight capture groups (e.g. with brackets around them) in the entire match output in Perl

I have the following code which successfully prints out all of the strings that match my regex into the console (perl myscript.pl sample_text.txt). $filename=shift; open text, $filename or die "error opening $filename\n"; while (my $line = )…
votresignu
  • 71
  • 4
2
votes
0 answers

Using re.sub with capture group references and numbers

Consider this simple MWE: s = 'foo bar bar' I'd like to use re.sub to change this to, say: t = 'foo1 bar1 bar1' If I wanted to append a suffix X, this would be pretty simple: re.sub(r'(\w+)\b', r'\1{}'.format('X'), s) 'fooX barX barX' However,…
cs95
  • 274,032
  • 76
  • 480
  • 537
2
votes
0 answers

Is there a downside to using named, rather than numbered, capture groups in Java?

I tend to prefer named capture groups when I use regular expressions. I find that they eliminate a lot of confusion, so that rather than needing to figure out the number of groups preceding your group, you just give a group its own label and can…
Tim M.
  • 518
  • 4
  • 15
2
votes
2 answers

awk - parse text having same character in fields as delimiter

Consider this source: field1;field2;"data;data field3";field4;"data;data field5";field6 field1;"data;data field2";field3;field4;field5;"data;data field6" As you can see, the field delimiter is being used inside certain fields, enclosed between ". I…
one-liner
  • 761
  • 1
  • 8
  • 19
2
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
  • 589
  • 1
  • 6
  • 25
2
votes
2 answers

ruby regex scan and gsub work differently with capture groups in blocks

I have a string of alternating digits and letters. I want to replace each character with the number of letters preceding it. For example, 2a3b should return aabbb. First, If I do: "2a3b".scan(/(\d)(.)/) do |count, char| puts char * count.to_i end…
Anand
  • 3,246
  • 4
  • 28
  • 55
1 2
3
10 11