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
1 answer

Kodos & "cannot refer to open group"

I want to only match 1010 or 0101 but nor 1111 nor 0000. I use the following regex : \b((1|0)(?!\2))+ It works well in Kodos but I also want the matched sequence thanks to group(). I've tried : \b(((1|0)(?!\2))+) but "cannot refer to open group*"…
lilawood
  • 2,263
  • 5
  • 22
  • 27
4
votes
1 answer

Perl 6 capturing repeating matching groups separately?

I believe Perl 6 offers the capability of capturing repeating groups separately as opposed to earlier flavors where you could only capture the last group or the whole matched group string. Can someone please give a good example how to use this…
bootkick
  • 482
  • 1
  • 5
  • 18
3
votes
2 answers

RegExp: how to exclude matched groups from $N?

I've made a working regexp, but i think it's not the best use-case: el = '
123
'; el.replace(/()(\d+)(<\/div>)/g, '$1$2$3'); // expecting result:
123
After googling i've…
mjey
  • 183
  • 2
  • 9
3
votes
4 answers

Working with Regular Expressions - Repeating Patterns

I am trying to use regular expressions to match some text. The following pattern is what I am trying to gather. @Identifier('VariableA', 'VariableB', 'VariableX', ..., 'VariableZ') I would like to grab a dynamic number of variables rather than a…
Michael
  • 161
  • 3
  • 9
3
votes
0 answers

Behaviour of the ? quantifier when applying to (\b) in JavaScript regex

I have a tiny regex: foo(\b)?. This was meant to be an experiment to see if I can deduce the existence of the boundary just by checking whether the first group was matched (and resulting in an empty string) or not. I tried this with some languages:…
InSync
  • 4,851
  • 4
  • 8
  • 30
3
votes
1 answer

Regex to extract just first IP from different pattern events

I am trying to extract just the first IP from below events through regex - For: ${nd:app://${namingDetails}.for.net.sh}, 4.5.3.2, 23.200.06.110 V- For: 10.10.21.17, 5.18.11.74, 23.36.3.22 V- For: 23.3.39.1, 21.61.39.21 V- For: 3.3.39.1 V- So if we…
ak9092
  • 73
  • 5
3
votes
2 answers

How do I get find a word that contains some specific letter AND one in particular (using regex)?

Hello everyone and thank you in advance, I am trying to get a all the words in the following list except for "motiu" and "diomar" using regex and…
3
votes
3 answers

Why does echo not return the same thing as without

I have the following case: regex: $'\[OK\][[:space:]]+([[:alnum:]_]+)\.([[:alnum:]_]+)([^[]*)' text: [OK] AAA.BBBBBB aaabbbcccdddfffed asdadadadadadsada [OK] CCC.KKKKKKK some text here [OK] OKO.II if I am using this site…
Mircea
  • 1,671
  • 7
  • 25
  • 41
3
votes
1 answer

Not sure why Regex.Replace() isn't working when using a Dictionary that holds Regex Patterns (involving capturing groups)

So I'm trying to write a way to replace "100¢" to "100 cents" using Regex. The pattern I'm using is (\d+)(¢). On top of that, I'm also trying to replace other things, so I would need a Dictionary data structure to hold all these regex patterns as…
3
votes
2 answers

Regular Expression to split text based on different patterns (within a single expression)

I have some patterns which detect questions and splits on top of that. there are some assumptions which I'm using like: Every pattern starts with a \n Every pattern ends with \s+ And how I define a pattern is like: . Q . Q…
Deshwal
  • 3,436
  • 4
  • 35
  • 94
3
votes
1 answer

Match specific string not preceded with a word and any amount of whitespaces using .NET regex

I am using Regex: (? 115 THEN 'greater' WHEN EMP_ID < 115 THEN 'lower' END AS TEST Matches: 1 match ( EMP_ID) But if I add any spaces after WHEN in this query, then it will show 2 matches of…
Walter
  • 133
  • 1
  • 10
3
votes
2 answers

Regex: Last Occurrence of a Repeating Character

So, I am looking for a Regex that is able to match with every maximal non-empty substring of consonants followed by a maximal non-empty substring of vowels in a String e.g. In the following strings, you can see all expected matches: "zcdbadaerfe" =…
3
votes
1 answer

Regex pattern to capture numbers only, on a single line, but exclude date pattern

Here is my current regex: \b\d(?:[^\n\d]*\d){14}(?!\d) Explanation: \b: Matches a word boundary so that first match digit is matched in a separate word [^\n\d]*: Matches 0 or more of any char that is not a digit and not a line…
DanielAttard
  • 3,467
  • 9
  • 55
  • 104
3
votes
1 answer

How to edit lines of a text file based on regex conditions?

import re re_for_identificate_1 = r"" with open("data_path/filename_1.txt","r+") as file: for line in file: #replace with a substring adding a space in the middle line = re.sub(re_for_identificate_1, " milesimo", line) …
3
votes
1 answer

Regex to individuate entries separate by specific character

I'm using Google Sheets and attempting to individuate the entries separated by [char10] > Sample content from a cell: > low confidence registrar [char10] > No SSL certificate [char10] > Malicious intent [char10] > Complete sentence #4…