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

Remove all numbers + symbols from line in Notepad++

Is it possible to remove every line in a notepad++ Not Containing a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U …
moon93
  • 129
  • 3
  • 11
4
votes
1 answer

Process Date Regex Capturing Groups outputs in R

I'm trying to coerce dates from two formats into a single one that I can easily feed into as.Date. Here's a sample: library(dplyr) df <- data_frame(date = c("Mar 29 2017 9:30AM", "5/4/2016")) I've tried this: df %>% mutate(date =…
Zafar
  • 1,897
  • 15
  • 33
4
votes
2 answers

Python regex vs Regex101

Input string: I0419 01:52:16.606123 136 TrainerInternal.cpp:181] Pass=15 Batch=74 samples=3670 AvgCost=263.331 Eval: classification_error_evaluator=0.970178 I0419 01:52:16.815407 136 Tester.cpp:115] Test samples=458 cost=203.737 Eval:…
alvas
  • 115,346
  • 109
  • 446
  • 738
4
votes
1 answer

Regex to find matches from a string having multiple square brackets

I have a paragraph like below: Some wording for testing [!#today] where the [!condition] does not satisfy with this verbiage [!ShowElemIf://Student/FullName; [[[Text not recognized fully]]] ;/First Name] But simple tags found having age…
sapatelbaps
  • 484
  • 2
  • 8
  • 19
4
votes
2 answers

How to exclude first group's single character from being matched into second group?

I would like to build q regular expression that matches patterns of repeated single characters, followed by each other. For example three times the same character 'A' followed by two times another character 'B'. It doesn't matter if the second…
tbop
  • 394
  • 1
  • 3
  • 13
4
votes
2 answers

Python Regular Expression named groups

Im trying to build a regular expression that captures any number (integer, float, with scientific notation or not). Im using groups so that if I need to update something I update only one line. Here's what I'm doing: intNumber =…
capitan
  • 309
  • 4
  • 13
4
votes
1 answer

How to delimit a regex group number in notepad++ replacement?

I want to specify a captured group then a literal digit in a replacement term, but the literal digit is being interpreted as part of the group number. Given this (contrived) example: Input text: A5 Find: (.)(.) Replace: $16 Expected result:…
Bohemian
  • 412,405
  • 93
  • 575
  • 722
4
votes
4 answers

How to capture all regex groups in one regex?

Given a file like this: # For more information about CC-CEDICT see: # http://cc-cedict.org/wiki/ A A [A] /(slang) (Tw) to steal/ AA制 AA制 [A A zhi4] /to split the bill/to go Dutch/ AB制 AB制 [A B zhi4] /to split the bill (where the male counterpart…
alvas
  • 115,346
  • 109
  • 446
  • 738
4
votes
4 answers

How to make a group for each word in a sentence?

This may be a silly question but... Say you have a sentence like: The quick brown fox Or you might get a sentence like: The quick brown fox jumped over the lazy dog The simple regexp (\w*) finds the first word "The" and puts it in a group. For…
blah238
  • 1,796
  • 2
  • 18
  • 51
4
votes
1 answer

Capture group multiple times

Lately I have being playing around with regex in Java, and I find myself into a problem which (theoretically) is easy to solve, but I was wandering if there is any easier way to do it (Yes, yes I am lazy), the problem is capture a group multiple…
Ordiel
  • 2,442
  • 3
  • 36
  • 52
4
votes
2 answers

Regular expression: Define max length of string, with repeated groups

The title might be confusing, but I will try to use some examples to explain. This is the current expression I've made. ^([A-ZÆØÅ][a-zæøå]+[\s-]{1}){2,20}$ I want an expression that will match these: So the general rule is, -Every word has to start…
Patidati
  • 1,048
  • 2
  • 12
  • 19
4
votes
2 answers

Linux SED RegEx replace, but keep wildcards

If I have a string that contains this somewhere (Foo could be anything): Foo How would I, using SED and RegEx, replace it with this: [tag]Foo[/tag] My failed attempt: echo "Foo" | sed "s/\(.*\)<\\/tag>/[tag]\1[\\/tag]"
david
  • 292
  • 5
  • 20
4
votes
1 answer

Match regex groups to list in java (Hearst Pattern)

I'm trying to match Hearst-Patterns with Java regex this is my regex: (\w+)<\/np> such as (?:(?:, | or | and )?(\w+)<\/np>)* If I have a annotated sentence like: I have a car such as BMW, Audi or Mercedes
tbraun89
  • 2,246
  • 3
  • 25
  • 44
4
votes
2 answers

BASH regex match MAC address

I'm trying to allow a user to only input a valid mac address (i.e. 0a:1b:2c:3d:4e:5f), and would like it to be more succinct than the expanded form: [[ $MAC_ADDRESS =~…
user2988671
  • 43
  • 1
  • 1
  • 3
4
votes
1 answer

regex for date omit one character of month part

It's very strange because it is very simple regex for dd/mm format. The result should be: "Group 1: 14; Group 2: 12" but it is "Group 1: 14; Group 2: 1". The 2nd group only captured the first character, but omit the second one ('2' in the…
Trang
  • 41
  • 2