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
2 answers

Convert regex Captures into HashMap in Rust?

I have a Regex with an unknown number of named groups with unknown names. I want to match a string to that regex, and get a HashMap<&str, &str> with the name of the groups as key and the captured strings as value. How can I do this? Will I have to…
Anders
  • 8,307
  • 9
  • 56
  • 88
5
votes
4 answers

Regex Remove white spaces from a group

Hi I have the following values 000001010016C02AB 111* 000001010016C02 111H 000001010016C 111 And the expected output is 00000101001,C02AB,* 00000101001,C02,H 00000101001,C, The values might vary.The length of this string will always be…
Prakash
  • 139
  • 1
  • 9
5
votes
2 answers

Replace all occurrences of the Tab character within double quotes

In the end, I will want to replace all the \t that are enclosed within " I'm currently on Regex101 trying various iterations of my regex... This is the the closest I have so far... originString =…
blaze_125
  • 2,262
  • 1
  • 9
  • 19
5
votes
1 answer

How can I force two patterns to capture to the same group in regEx

I have to parse a string, formatted as!info1!info2!, and info2 is optional. I am trying to capture, using a regular expression, info1 and info2 if needed. I came up with the following pattern: !([^!]*?)!(.*?)!|!(.*?)! It works, but I am not…
Maxime
  • 1,245
  • 2
  • 14
  • 24
5
votes
1 answer

Tell RegEx to ignore parenthesis when inside a quote

I have the following RegEx that is used and works: /\B@(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x Where this string @extends('template', 'test') correctly groups and gives me what I need. The problem is if the string contains an…
Laurence
  • 58,936
  • 21
  • 171
  • 212
5
votes
2 answers

How exactly does this recursive regex work?

This is a followup to this question. Have a look at this pattern: (o(?1)?o) It matches any sequence of o with a length of 2n, with n ≥ 1. It works, see regex101.com (word boundaries added for better demonstration). The question is: Why? In the…
Imanuel
  • 3,596
  • 4
  • 24
  • 46
5
votes
1 answer

Regex match and replace operators in math operation

Given an input string 12/3 12*3/12 (12*54)/(3/4) I need to find and replace each operator with a string that contains the operator some12text/some3text some12text*some2text/some12text (some12text*some54text)/(some3text/some4text) practical…
Ash M
  • 1,399
  • 2
  • 10
  • 23
5
votes
2 answers

How to capture the entire string while using 'lookaround' with chars in regex?

I have to find all strings which are made of only letters 'a' and 'b' and every instance of 'a' is immediately followed by 'b' and immediately preceded by 'b'. For example: mystring = 'bab babab babbab ab baba aba xyz' Then my regex should…
5
votes
1 answer

regex capture group in mysql

Is it possible to capture the regex match in the select part of the query in mysql? I'd like to query for an initial letters in the UK postcode like: SELECT all initiall letters from a post code (one or two) FROM addresses; UK postcodes start with…
sumek
  • 26,495
  • 13
  • 56
  • 75
5
votes
5 answers

Perl multiline regex

I have a file full of json objects to parse, similar to this one: { "_id" : ObjectId("523a58c1e4b09611f4c58a66"), "_items" : [ { "adGroupId" : NumberLong(1230610621), "keywordId" : NumberLong("5458816773") }, { …
5
votes
4 answers

using regular expression substitution command to insert leading zeros in front of numbers less than 10 in a string of filenames

I am having trouble figuring out how to make this work with substitution command, which is what I have been instructed to do. I am using this text as a variable: text = 'file1, file2, file10, file20' I want to search the text and substitute in a…
kflaw
  • 424
  • 1
  • 10
  • 26
5
votes
1 answer

Regex, group & quantifyer

I just did the funny regex crosswords at http://regexcrossword.com/ - and found out I don't understand what quantifying groups means, e.g. (.)+ or (.)* Let me try at http://ole.michelsen.dk/tools/regex.html , it offers the JavaScript and the PHP…
Falko
  • 1,028
  • 1
  • 12
  • 24
5
votes
1 answer

RegEx for extracting a value from Open3.popen3 stdout

How do I get the output of an external command and extract values from it? I have something like this: stdin, stdout, stderr, wait_thr = Open3.popen3("#{path}/foobar", configfile) if /exit 0/ =~ wait_thr.value.to_s runlog.puts("Foobar exited…
Astaar
  • 5,858
  • 8
  • 40
  • 57
5
votes
1 answer

Boost regex. Named group in two part

I have problem with boost::regex::regex_match. I work with turned on BOOST_REGEX_MATCH_EXTRA. What I have: (this is a simple example of my problem, not a real task) string input1= "3 4 5"; string input2= "3 4 7"; What I want to get: list output1=…
Darida
  • 196
  • 1
  • 8
5
votes
1 answer

RegEx for fulltext search with typos

I have a MySQL table with the following columns: City Country Continent New York States Noth America New York Germany Europe - considering there's one ;) Paris France Europe If I want to find "New Yokr" with a typo, it's easy with…
TrOnNe
  • 1,632
  • 16
  • 30