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
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
  • 6
4
votes
1 answer

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
  • 12,091
  • 8
  • 72
  • 95
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
  • 37,407
  • 44
  • 153
  • 217
4
votes
1 answer

Get error when try to use `\1` which get from regex `(\d)?` in Python

Example code: #!/usr/bin/env python import re print re.sub(r'a+(\d)?', r'\1', "aaaa3") print re.sub(r'a+(\d)?', r'\1', "aaaa") # error! The second print statement gives me an error: 3 Traceback (most recent call last): File "./bbb.py", line 5,…
fronthem
  • 4,011
  • 8
  • 34
  • 55
4
votes
1 answer

Why is my regex capture group only capturing the last part of the string when it matches multiple parts?

What I Tried var test = "asdfdas ABCD EFGH"; var regex = /^\S+( [A-Z]{4})+$/; // Also tried: /^\S+( [A-Z]{4})+$/g // And: /^\S+( [A-Z]{4})+?$/g var matches = test.match(regex); I made a JSFiddle. What I Expect The variable matches should…
NoBrainer
  • 5,853
  • 1
  • 27
  • 27
4
votes
2 answers

Do the following capture group notation mean something to Perl

In the following why does the condition evaluate to false? $_ = "aa11bb"; if(/(.)\111/){ print "It matched!\n"; } Does \11 or \111 have special meaning that Perl can not "see" \1?
Jim
  • 18,826
  • 34
  • 135
  • 254
3
votes
1 answer

Replace multiple captured groups in regex

VB2005: I've been looking at regex for some hours now and cant seem to get my head around the .Replace for my case. I'm looking for two fields and then I want to replace those fields with new values. So my string looks like so: Dim myInputString as…
sinDizzy
  • 1,300
  • 7
  • 28
  • 60
3
votes
7 answers

How to capture one digit with sed and replace the other?

In a text file test.txt are many lines of text, of which I want to extract a single line matching: blabla 28.40.00 blabla I would like to replace the first digit of the middle number (in this case 4) by three. That is, no matter what the middle…
learning2code
  • 460
  • 4
  • 9
3
votes
1 answer

Regex capture group works in Javascript and regex101, but not in sed

In regex101: https://regex101.com/r/FM88LA/1 In my browser console: x='"AbCd123|999"'; "\"AbCd123|999\"" x.match(/[^\""|]+/) Array [ "AbCd123" ] Using sed in the shell: (base) balter@winmac:~/winhome/CancerGraph/TCGA$ echo '"AbCd123|99999"' | sed…
abalter
  • 9,663
  • 17
  • 90
  • 145
3
votes
1 answer

Regex capture groups for "timer" sentence pattern

I am trying to write a regular expression that will allow me to parse and modify strings that may include an instruction to time a specific action, using capture groups to identify "hour" "minute" and "second" values in the input string. In ruby I…
Austin Meyers
  • 153
  • 2
  • 12
3
votes
3 answers

How can I get the count of a capture group then replace the characters with a specific character?

How can I get the count of a capture group and replace it with the same number of characters that I specify? For example here is a string... 123456789ABCD00001DDD My regex with capture groups is as…
Arvo Bowen
  • 4,524
  • 6
  • 51
  • 109
3
votes
1 answer

Java Regex - capture string with single dollar, but not when it has two successive ones

I posted this question earlier. But that wasn't quite the end of it. All the rules that applied there still apply. So the strings: "%ABC%" would yield ABC as a result (capture stuff between percent signs) as would "$ABC." (capture stuff after $,…
JGFMK
  • 8,425
  • 4
  • 58
  • 92
3
votes
4 answers

regex replace parts/groups of a string in R

Trying to postprocess the LaTeX (pdf_book output) of a bookdown document to collapse biblatex citations to be able to sort them chronologically using \usepackage[sortcites]{biblatex} later on. Thus, I need to find }{ after \\autocites and replace it…
rnuske
  • 83
  • 7
3
votes
1 answer

Regex to capture unknown number of repeated groups

I'm try to write a regular expression to use in a Java program that will recognize a pattern that may appear in the input an unknown number of times. My silly little example is: String patString = "(?:.*(h.t).*)*"; Then I try to access the matches…
umbraphile
  • 377
  • 2
  • 5
  • 15
3
votes
2 answers

Python Regular Expression Named Capture Groups

Im learning regular expressions, specifically named capture groups. Having an issue where I'm not able to figure out how to write an if/else statement for my function findVul(). Basically how the code works or should work is that findVul() goes…
kawhi_l02
  • 81
  • 1
  • 5
1
2
3
12 13