Skip to main content
Filter by
Sorted by
Tagged with
2498 votes
18 answers
1.3m views

How are non-capturing groups, i.e., (?:), used in regular expressions and what are they good for?
never_had_a_name's user avatar
360 votes
3 answers
297k views

In Python, the (?P<group_name>…) syntax allows one to refer to the matched string through its name: >>> import re >>> match = re.search('(?P<name>.*) (?P<phone>.*)'...
Eric O. Lebigot's user avatar
304 votes
4 answers
156k views

How can I replace foobar with foo123bar? This doesn't work: >>> re.sub(r'(foo)', r'\1123', 'foobar') 'J3bar' This works: >>> re.sub(r'(foo)', r'\1hi', 'foobar') 'foohibar'
zhigang's user avatar
  • 7,283
260 votes
19 answers
420k views

I'm trying to parse the following kind of string: [key:"val" key2:"val2"] where there are arbitrary key:"val" pairs inside. I want to grab the key name and the value. For those curious I'm trying to ...
gatlin's user avatar
  • 2,619
252 votes
32 answers
310k views

I'm after a regex that will validate a full complex UK postcode only within an input string. All of the uncommon postcode forms must be covered as well as the usual. For instance: Matches CW3 9SS SE5 ...
Kieran Benton's user avatar
221 votes
5 answers
186k views

I have a list of words: bau ceu diu fou gau I want to turn that list into: byau cyeu dyiu fyou gyau I unsuccessfully tried the command: :%s/(\w)(\w\w)/\1y\2/g Given that this doesn't work, what do ...
Christian's user avatar
  • 26.5k
193 votes
2 answers
163k views

Is there any way to directly replace all groups using regex syntax? The normal way: re.match(r"(?:aaa)(_bbb)", string1).group(1) But I want to achieve something like this: re.match(r"(\d.*?)\s(\d.*?...
user avatar
166 votes
4 answers
150k views

I'm using rubular.com to build my regex, and their documentation describes the following: (...) Capture everything enclosed (a|b) a or b How can I use an OR expression without capturing what's in ...
goggin13's user avatar
  • 8,006
141 votes
2 answers
253k views

I am using this regex: ((?:[a-z][a-z]+))_(\d+)_((?:[a-z][a-z]+)\d+)_(\d{13}) to match strings like this: SH_6208069141055_BC000388_20110412101855 separating into 4 groups: SH 6208069141055 ...
joe's user avatar
  • 8,704
138 votes
8 answers
180k views

I have this code, and I want to know, if I can replace only groups (not all pattern) in Java regex. Code: //... Pattern p = Pattern.compile("(\\d).*(\\d)"); String input = "6 example input 4"; ...
wokena's user avatar
  • 1,379
116 votes
1 answer
157k views

Nginx regex location syntaxe Regex expressions can be used with Nginx location block section, this is implemented with the PCRE engine. What does exactly this feature support as it is not fully ...
intika's user avatar
  • 10k
61 votes
6 answers
21k views

if a string has this predicted format: value = "hello and good morning" Where the " (quotations) might also be ' (single quote), and the closing char (' or ") will be the same as the opening one. I ...
Yuval A.'s user avatar
  • 6,160
50 votes
5 answers
93k views

If I have the following pattern in some text: def articleContent = "<![CDATA[ Hellow World ]]>" I would like to extract the "Hellow World" part, so I use the following code to match it: def ...
RicardoE's user avatar
  • 1,735
46 votes
4 answers
48k views

I'm trying to use a regular expression to erase only the matching part of an string. I'm using the preg_replace function and have tried to delete the matching text by putting parentheses around the ...
PartySoft's user avatar
  • 2,849
44 votes
3 answers
36k views

I want to sanitise some input and replace several characters with acceptable input, e.g. a Danish 'å' with 'aa'. This is easily done using several statements, e.g. /æ/ae/, /å/aa/, /ø/oe/, but due to ...
Jan's user avatar
  • 903
39 votes
9 answers
37k views

I am stumped trying to create an Emacs regular-expression that excludes groups. [^] excludes individual characters in a set, but I want to exclude specific sequences of characters: something like [^(...
Anycorn's user avatar
  • 51.8k
39 votes
4 answers
16k views

Ok so I am trying to group past the 9th backreference in notepad++. The wiki says that I can use group naming to go past the 9th reference. However, I can't seem to get the syntax right to do the ...
Steven Combs's user avatar
  • 1,940
30 votes
2 answers
149k views

I have this regex (?:$|^| )(one|common|word|or|another)(?:$|^| ) which matches fine unless the two words are next to each other. One one's more word'word common word or another word more another ...
San's user avatar
  • 4,184
28 votes
6 answers
24k views

Does Kotlin have support for named regex groups? Named regex group looks like this: (?<name>...)
gs_vlad's user avatar
  • 1,459
26 votes
2 answers
34k views

Here are the cases. I'm looking for the following pattern in a log file. All strings are in the form of AB_N or CDE_N. AB and CDE are fixed letters, followed by an underscore. N can be either 2 or ...
pedram's user avatar
  • 3,117
25 votes
3 answers
5k views

My regex_replace expression uses group $1 right before a '0' character in the replacement string like so: #include <iostream> #include <string> #include <regex> using namespace std;...
srking's user avatar
  • 4,752
24 votes
3 answers
32k views

val REGEX_OPEN_CURLY_BRACE = """\{""".r val REGEX_CLOSED_CURLY_BRACE = """\}""".r val REGEX_INLINE_DOUBLE_QUOTES = """\\\"""".r val REGEX_NEW_LINE = """\\\n""".r // Replacing { with '{' and } with '}'...
yalkris's user avatar
  • 2,715
22 votes
6 answers
46k views

I have a regex: /abc(def)ghi(jkl)mno(pqr)/igs How would I capture the results of each parentheses into 3 different variables, one for each parentheses? Right now I using one array to capture all the ...
Incognito's user avatar
  • 1,953
18 votes
5 answers
6k views

I know that I can yank all matched lines into register A like this: :g/regex/y/A But I can't seem to figure out how to yank match regex groups into register A: :g/\(regex\)/\1y A (E10: \ should be ...
user2506293's user avatar
15 votes
2 answers
7k views

Is there a way to use same name in regex named group in python? e.g.(?P<n>foo)|(?P<n>bar). Use case: I am trying to capture type and id with this regex: /(?=videos)((?P<type>videos)/...
tsh's user avatar
  • 967
14 votes
1 answer
10k views

I try replace author Full name with article title I have a list of articles, similar like this: Albershein P., Nevis D. J. A method for analysis of sugars in plant cell wall polysaccharides by ...
Victor Dronov's user avatar
12 votes
3 answers
28k views

Is it possible to use regex global g flag in java pattern ? I tried with final Pattern pattern = Pattern.compile(regex,Pattern.DOTALL); but it do not behave like global flag. Do we have any ...
Sandeep Chauhan's user avatar
12 votes
1 answer
16k views

Please explain the meaning of this regular expression and what groups the expression will generate? $string =~ m/^(\d*)(?: \D.*?)(\d*)$/ PS: I'm re-factoring Perl code to Java.
Saravanan's user avatar
  • 149
11 votes
4 answers
49k views

Maybe my reasoning is faulty, but I can't get this working. Here's my regex: (Device\s#\d(\n.*)*?(?=\n\s*Device\s#|\Z)) Try it: http://regex101.com/r/jQ6uC8/6 $getdevice is the input string. I'm ...
Frankstar's user avatar
  • 336
11 votes
1 answer
20k views

I want to use regex group to replace string in golang, just like as follow in python: re.sub(r"(\d.*?)[a-z]+(\d.*?)", r"\1 \2", "123abc123") # python code So how do I implement this in golang?
roger's user avatar
  • 10.1k
11 votes
7 answers
7k views

I am writing a set of RegExps to translate a CSS selector into arrays of ids and classes. For example, I would like '#foo#bar' to return ['foo', 'bar']. I have been trying to achieve this with "#...
Alexandre Kirszenberg's user avatar
11 votes
3 answers
514 views

I want to match against a programmatically-constructed regex, containing a number of (.*) capture groups. I have this regex as a string, say my $rx = "(.*)a(.*)b(.*)" I would like to ...
grobber's user avatar
  • 1,143
10 votes
1 answer
1k views

I'm trying to come up with an example where positive look-around works but non-capture groups won't work, to further understand their usages. The examples I"m coming up with all work with non-...
Moondra's user avatar
  • 4,551
10 votes
2 answers
12k views

I've a string like this: docker login -u username -p password docker-registry-url. I execute the command in a Groovy script with execute. For debugging purposes, I print the command before execution, ...
Abhijit Sarkar's user avatar
10 votes
4 answers
497 views

In PCRE2 or any other regex engine supporting forward backreferences, is it possible to change a capture group that matched in a previous iteration of a loop into a non-participating capture group (...
Deadcode's user avatar
  • 868
10 votes
2 answers
331 views

With perl (and almost any regex flavour), every group is numbered sequentially. So for example, this code: 'bar' =~ m/(foo)|(bar)/; print $1 // 'x'; # (1-based index) print $2 // 'x'; # (1-based ...
Julio's user avatar
  • 5,316
10 votes
1 answer
12k views

Nginx uses the PCRE engine for evaluating regular expressions, the documentation state that / delimiter is not used so we don’t have to escape the forward slash / in an URI as we may do in a standard ...
intika's user avatar
  • 10k
9 votes
2 answers
11k views

I have few regular expression used for form validation and I noticed that my project is not accessible through firefox as it shows nothing! but give the error in the console, SyntaxError: invalid ...
Hasini Silva's user avatar
8 votes
2 answers
3k views

I would like to use matcher.group("login"); In android 8+ on eclipse, but no Matcher.group(String) seems to exist. Have you got a (built in) solution ?
hanoo's user avatar
  • 4,265
8 votes
1 answer
2k views

I want to capture groups matching a pattern where the input can contain this group one or more times. Example: input = 12361 randomstuff371 12 Mar 16 138more random381 stuff73f I want to capture the ...
J Z's user avatar
  • 99
8 votes
1 answer
506 views

How can I use capturing groups inside lookahead assertion? This code: say "ab" ~~ m/(a) <?before (b) > /; returns: 「a」 0 => 「a」 But I was expecting to also capture 'b'. Is there ...
Julio's user avatar
  • 5,316
7 votes
4 answers
9k views

Can anyone explain: Why the two patterns used below give different results? (answered below) Why the 2nd example gives a group count of 1 but says the start and end of group 1 is -1? public void ...
user358795's user avatar
7 votes
2 answers
13k views

Say I have a regular expression like the following, but I loaded it from a file into a variable $regex, and so have no idea at design time what its contents are, but at runtime I can discover that it ...
Hoobajoob's user avatar
  • 2,908
7 votes
1 answer
1k views

For all intents and purposes, I am a Python user and use the Pandas library on a daily basis. The named capture groups in regex is extremely useful. So, for example, it is relatively trivial to ...
user1718097's user avatar
  • 4,332
7 votes
3 answers
7k views

I'm trying to write some regex that will allow me to do a negative lookbehind on a capture group so that I can extract possible references from emails. I need to know how to look behind from a certain ...
Lank's user avatar
  • 75
7 votes
5 answers
2k views

I have some programmatically assembled huge regex, like this (A)|(B)|(C)|... Each sub-pattern is in its capturing group. When I get a match, how do I figure out which group matches without linearly ...
Fortepianissimo's user avatar
6 votes
3 answers
5k views

I got this problem. I have a val line:String = "PE018201804527901" that matches with this regex : (.{2})(.{4})(.{9})(.{2}) I need to extract each group from the regex to an Array. The result ...
Will's user avatar
  • 145
6 votes
2 answers
3k views

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 ...
Anders's user avatar
  • 8,663
6 votes
3 answers
751 views

a <- c("this is a number 9999333333 and i got 12344") How could i replace the number greater than 5 digits with the extra digits being "X" Expected Output: "this is a ...
prog's user avatar
  • 1,073
6 votes
2 answers
1k views

I was trying to remove duplicate words from a string in scala. I wrote a udf(code below) to remove duplicate words from string: val de_duplicate: UserDefinedFunction = udf ((value: String) => { ...
Vaibhav 's user avatar

1
2 3 4 5
56