Questions tagged [regex-group]

Regex groups are created by placing part of a regular expression inside parentheses. Groups allows to apply a quantifier to the entire group or to restrict alternation to part of the regex. Besides grouping part of a regular expression together, parentheses also create a numbered capturing group. It stores the part of the string matched by the part of the regular expression inside the parentheses.

The regex Set(Value)? matches Set or SetValue. In the first case, the first (and only) capturing group remains empty. In the second case, the first capturing group matches Value.

If capturing the match isn't needed, the regular expression can be optimized into Set(?:Value)?. The question mark and the colon after the opening parenthesis are the syntax that creates a non-capturing group.

The question mark after the opening bracket is unrelated to the question mark at the end of the regex. The final question mark is the quantifier that makes the previous token optional. This quantifier cannot appear after an opening parenthesis, because there is nothing to be made optional at the start of a group. Therefore, there is no ambiguity between the question mark as an operator to make a token optional and the question mark as part of the syntax for non-capturing groups.

2670 questions
4
votes
2 answers

Perl switch/case Fails on Literal Regex String Containing Non-Capturing Group '?'

I have text files containing lines like: 2/17/2018 400000098627 =2,000.0 $2.0994 $4,387.75 3/7/2018 1)0000006043 2,000.0 $2.0731 $4,332.78 3/26/2018 4 )0000034242 2,000.0 $2.1729 $4,541.36 4/17/2018 2)0000008516 2,000.0 $2.219 $4,637.71 I am…
Matthew
  • 757
  • 11
  • 19
4
votes
3 answers

Overlapping group capturing

Please take a look at the following code: public static void main(String[] args) { String s = "a < b > c > d"; String regex = "(\\w\\s*[<>]\\s*\\w)"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(s); int i = 0; …
Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
4
votes
1 answer

Regex Replace function: in cases of no match, $1 returns full line instead of null

Test link: regexr.com/42d9c This has been driving me crazy. I want to extract barcodes in the lines below: Ceres Juice Apricot 12 x 1lt unit: 6001240102022 Ceres Juice Cranberry& Kiwi Juice 12 x 1lt... unit: Ceres Juice Guava 12 x 1lt..…
Richard Woolf
  • 559
  • 2
  • 6
  • 19
4
votes
2 answers

A regex to find keywords in a import statement

Let's pretend we have the following import statements (not language-specific): import "./test" import "./test" as Test import { Test } from "./test" import { Person as Individual } from "./test" I'm trying to write a regular expression to…
4
votes
1 answer

Java Regex. group excluding delimiters

I'm trying to split my string using regex. It should include even zero-length matches before and after every delimiter. For example, if delimiter is ^ and my string is ^^^ I expect to get to get 4 zero-length groups. I can not use just regex =…
00vk
  • 65
  • 4
4
votes
3 answers

How can I extract these multiple regex groups in R

I have string inputs in the following format: my.strings <- c("FACT11", "FACT11:FACT20", "FACT1sometext:FACT20", "FACT1text with spaces:FACT20", "FACT14:FACT20", "FACT1textAnd1312:FACT2etc", "FACT12:FACT22:FACT31") I would like to extract all the…
bobbel
  • 1,983
  • 6
  • 21
4
votes
1 answer

Capture pattern group in case statements in bash scripts

A typical case statement in bash could be: k=abcdef case $k in "abc"* ) echo "pattern found" ;; esac One of my variables have the pattern key=value, and I wish to extract the value from it. k="abc=value1" case $k in "abc="*…
John Strood
  • 1,859
  • 3
  • 26
  • 39
4
votes
2 answers

Regex match everything between two {}

I was looking at different answers here but unfortunately none of them was good for my case. So I hope you don't mind about it. So I need to match everything between two curly brackets {} except situation when match starts with @ and without these…
Panczo
  • 424
  • 5
  • 20
4
votes
2 answers

Use previous backreference as name of named capture group

Is there a way to use a backreference to a previous capture group as the name of a named capture group? This may not be possible, if not, then that is a valid answer. The following: $data = 'description: some description'; preg_match("/([^:]+):…
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
4
votes
1 answer

Does an atomic quantified group mean the same as a quantified atomic group?

I was looking at this answer to this question: Regex nested parentheses, and was thinking that instead of a quantified atomic group (?> list | of | alternates )* it should have been an atomic quantified group (?> (?: list | of | alternates )* ). Am…
Adrian
  • 10,246
  • 4
  • 44
  • 110
4
votes
3 answers

Warning about uninitailized variable despite check for undef

I seem to be very silly today, or I have a very weird problem. Please consider the following code: #!/usr/bin/perl use strict; use warnings; use warnings::unused; use warnings FATAL => 'uninitialized'; my ($String, $Pattern); $Pattern =…
Binarus
  • 4,005
  • 3
  • 25
  • 41
4
votes
1 answer

Regex to match number with different digits and minimum length

I am trying to write a regex (to validate a property on a c# .NET Core model, which generates javascript expression) to match all numbers composed by at least two different digits and a minimum length of 6 digits. For example: 222222 - not…
4
votes
1 answer

IntelliJ Vim plugin doesn't support capture groups?

I'm beginning to use IntelliJ with the vim plugin, and found that commands as simple as /case (.*) fail to return any results. Of course, that's not a very useful command. I'd like to reverse the order of all of the elements of my table by…
Alex Sage
  • 45
  • 1
  • 4
4
votes
1 answer

How can I get multiple Java regex matches on only certain lines

There is an API that I'm calling which I cannot change. That is, I cannot do this as two sequential regexes or anything like that. The API is written something like this (simplified, of course): void apiMethod(final String regex) { final String…
4
votes
2 answers

Regex detect any repeated character but with optional whitespace between

So currently I've got the following regex pattern, allowing me to detect any string containing 9 characters that are the same consecutively. /^.*(\S)\1{9,}.*$/ This works perfectly with a string like the following: this a tesssssssssst however I…
Matt Cowley
  • 2,164
  • 2
  • 18
  • 29