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

emacs function re-search-forward interpreting \( \) group characters literally in regexp

I successfully used replace-regexp interactively to replace every instance of quoted text in the buffer shown below with a non-quoted version. The regexp I searched for was \"\([^\"]*\)\" and the NEWTEXT I inserted was \1. * "PROJECT…
user1593649
  • 115
  • 4
5
votes
1 answer

JavaScript regex back references returning an array of matches from single capture group (multiple groups)

I'm fairly sure after spending the night trying to find an answer that this isn't possible, and I've developed a work around - but, if someone knows of a better method, I would love to hear it... I've gone through a lot of iterations on the code,…
Patrick
  • 3,490
  • 1
  • 37
  • 64
4
votes
5 answers

How can I tell which of the alternatives matched in a Perl regular expression pattern?

I have a list of regular expressions (about 10 - 15) that I needed to match against some text. Matching them one by one in a loop is too slow. But instead of writing up my own state machine to match all the regexes at once, I am trying to | the…
dividebyzero
  • 1,243
  • 2
  • 9
  • 17
4
votes
4 answers

Merge two regexes with variable number of capture groups

I'm trying to match either (\S+)(=)([fisuo]) or (\S+)(!) And then have the results placed in a list (capture groups). All of my attempts result in extra, unwanted captures. Here's some code: #!/usr/bin/perl #-*- cperl -*- # $Id: test7,v 1.1…
Erik Bennett
  • 1,049
  • 6
  • 15
4
votes
1 answer

why do @- and @+ have different sizes after perl regex match

I expected @- and @+ to be the same size after a successful match, but they aren't. For example this script: #!/usr/bin/perl -w use strict; use warnings; use v5.20.0; use Data::Dumper; $Data::Dumper::Terse = 1; my $rex = '([a-z]+) |…
Britton Kerin
  • 427
  • 4
  • 9
4
votes
1 answer

Parse Address using Regex Capture Groups

I am trying to parsing the addresses into groups and I have this regular expression: (^.*?(?:Lane|Street|Boulevard|Crescent|Place|Road|Highway|Avenue|Drive|Circuit|Parade|Telopea|Nicklin Way|Terrace|Square|Court|Close|Endeavour…
dougj
  • 135
  • 3
  • 15
4
votes
1 answer

Is there a way to set the precedence of capturing groups

I want 3 capturing groups (pre/mid/post) that share some characters. All three can match 0 or 1 time, something like this: ^(?
[T])?(?[T][A-Z])?(?[T])?$

I am looking for them to match in order: mid -> pre -> post. Regex I've posted…
rybaczewa
  • 43
  • 3
4
votes
2 answers

Regex in Java use same group in alternative matches

I have a regex pattern which matches different expressions but i want both matches to be in the same capturing group. Currently i got the following regex: -([^\s"]+)|-"(.+?)" This does in fact match both (-hello -"Hello World") but in different…
Joel
  • 138
  • 10
4
votes
2 answers

Regex match every string inside double quotes and include escaped quotation marks

There are quite a few similar questions already but none of them works in my case. I have a string that contains multiple substrings inside double quotes and these substrings can contain escaped double quotes. For example for the string 'And then,…
Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
4
votes
2 answers

Regex matching a word containing a character exactly two times in a row

The problem As stated in the title, my goal is to find a regex that matches a word if and only if it contains a substring of exactly two consecutive characters which is not surrounded by that same character. Test cases Helo --> false programming…
4
votes
2 answers

Regex for repeating series of numbers and number ranges (e.g. 3 digit numbers and 3 digit number ranges)

I'm looking for a regex to match repeating number sequences. The number/range itself could be any three digit number, for e.g. I want to match 345 346-348 234,235,236,237-239 234, 235, 236, 237-239 234,234, 236 and 237-239 234,234, 236 or…
Bob76
  • 477
  • 1
  • 5
  • 12
4
votes
1 answer

Regex return specified number of characters before and after match

I have a requirement to extract the number of characters before and after REGEX match. For example: Input : ABCDEFGHIJK//MNOPQRST Output : IJK//MNOPQ Input : zzzABCDEFGHIJK//MNOPQRST Output : I want only first 3 characters before "//" and 5…
4
votes
2 answers

Dangling metacharacter * sparksql

Below regex works in Hive but not in Spark. It throws an error dangling metacharacter * at index 3: select regexp_extract('a|b||c','^(\\|*(?:(?!\\|\\|\\w(?!\\|\\|)).)*)'); I also tried escaping * with \\* but still it throws dangling metacharacter…
4
votes
1 answer

Is there a delimiter/disambiguation syntax for submatch group references in JavaScript string replacement?

This must have an answer somewhere already, but I don't even know what to search for. In JavaScript, I can reference a parenthesized submatch in a replacement string like so: "abcdefghijklmnopqrstuvwxyz".replace(/(.)/g, "$1-"); // Result:…
cdauth
  • 6,171
  • 3
  • 41
  • 49
4
votes
2 answers

Regex to find a multi line string that includes another string between lines

my first Q here. I have a log file that has multiple similar strings as hits: Region: AR OnlineID: Atl_Tuc ---Start--- FIFA 18 Legacy Edition ---END--- Region: FR OnlineID: jubtrrzz ---Start--- FIFA 19 Undertale Pro Evolution Soccer™…
morez890
  • 57
  • 6