Questions tagged [backreference]

Back references are regular expression constructs that make use of capturing in regex to perform replacement based on parts of the matched string captured during a match sequence in the regexp pattern.

Back references and s are regular expression constructs that makes use of capturing in to perform matching and replacement that remembers parts of the matched string during a match sequence in the regexp pattern.

The back-referencing constructs present in all engines are \1 to \9, where \1 back-reference references the first ( ) capturing group.

Read more:

392 questions
13
votes
4 answers

Named backreferences with preg_replace

Pretty straightforward; I can't seem to find anything definitive regarding PHP's preg_replace() supporting named backreferences: // should match, replace, and output: user/profile/foo $string = 'user/foo'; echo…
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
13
votes
4 answers

Extract capture group matches from regular expressions? (or: where is gregexec?)

Given a regular expression containing capture groups (parentheses) and a string, how can I obtain all the substrings matching the capture groups, i.e., the substrings usually referenced by "\1", "\2"? Example: consider a regex capturing digits…
Ferdinand.kraft
  • 12,579
  • 10
  • 47
  • 69
12
votes
1 answer

Backreferences in lookbehind

Can you use backreferences in a lookbehind? Let's say I want to split wherever behind me a character is repeated twice. String REGEX1 = "(?<=(.)\\1)"; // DOESN'T WORK! String REGEX2 = "(?<=(?=(.)\\1)..)"; // WORKS! …
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
12
votes
2 answers

preg_replace: add number after backreference

Situation I want to use preg_replace() to add a digit '8' after each of [aeiou]. Example from abcdefghij to a8bcde8fghi8j Question How should I write the replacement string? // input string $in = 'abcdefghij'; // this obviously won't work…
MightyPork
  • 18,270
  • 10
  • 79
  • 133
10
votes
1 answer

What does the "+0" mean in the regexp \k?

I'm new to Regular Expressions in Ruby, and I can't seem to find any solid documentation on what \k means. It's the +0 part that I'm not getting. Here's an example - this Regexp matches…
10
votes
2 answers

Delphi TRegEx backreference broken?

I have a problem using TRegEx.replace: var Value, Pattern, Replace: string; begin Value := 'my_replace_string(4)=my_replace_string(5)'; Pattern := 'my_replace_string\((\d+)\)'; Replace := 'new_value(\1)'; Value := TRegEx.Replace(Value,…
Imanuel
  • 3,596
  • 4
  • 24
  • 46
9
votes
2 answers

How to use back-reference of sed replacement command correctly considering a special Regular Expression

I am learning the sed s/regexp/replacement/ command on linux. There are some numbers from phone.txt (555)555-1212 (555)555-1213 (555)555-1214 (666)555-1215 (777)555-1217 I'd like to use the regular expression (which I have tested on…
David17
  • 173
  • 1
  • 1
  • 9
8
votes
4 answers

How to replace multiple matches / groups with regexes?

Normally we would write the following to replace one match: namesRegex = re.compile(r'(is)|(life)', re.I) replaced = namesRegex.sub(r"butter", "There is no life in the void.") print(replaced) output: There butter no butter in the void. What i want…
KeyC0de
  • 4,728
  • 8
  • 44
  • 68
8
votes
3 answers

PHP regex and adjacent capturing groups

I'm using capturing groups in regular expressions for the first time and I'm wondering what my problem is, as I assume that the regex engine looks through the string left-to-right. I'm trying to convert an UpperCamelCase string into a…
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
8
votes
2 answers

Seperate backreference followed by numeric literal in perl regex

I found this related question : In perl, backreference in replacement text followed by numerical literal but it seems entirely different. I have a regex like this one s/([^0-9])([xy])/\1 1\2/g ^ whitespace here But…
udiboy1209
  • 1,472
  • 1
  • 15
  • 33
7
votes
2 answers

Regex; backreferencing a character that was NOT matched in a character set

I want to construct a regex, that matches either ' or " and then matches other characters, ending when a ' or an " respectively is matched, depending on what was encountered right at the start. So this problem appears simple enough to solve with the…
flamming_python
  • 690
  • 7
  • 13
7
votes
4 answers

Alphabetic order regex using backreferences

I recently came across a puzzle to find a regular expression that matches: 5-character-long strings comprised of lowercase English letters in ascending ASCII order Valid examples include: aaaaa abcde xxyyz ghost chips demos Invalid examples…
Jedi
  • 3,088
  • 2
  • 28
  • 47
7
votes
1 answer

Backreference in R

I got really confused about the usage of backreferences strings <- c("^ab", "ab", "abc", "abd", "abe", "ab 12") gsub("(ab) 12", "\\1 34", strings) [1] "^ab" "ab" "abc" "abd" "abe" "ab 34" gsub("(ab)12", "\\2 34", strings) [1] "^ab" …
Bratt Swan
  • 1,068
  • 3
  • 16
  • 28
7
votes
1 answer

Java regex error - Look-behind with group reference

I'm trying to build a regex that matches exactly two occurrences of a char in a class. This is the regex I made: (?
6
votes
4 answers

Python - Modifying a backreference. Can it be done?

New to Python so please forgive my ignorance. I'm trying to modify backreferenced strings in a regular expression. Example: >>>a_string 'fsa fad fdsa dsafasdf u.s.a. U.S.A. u.s.a fdas adfs.f fdsa f.afda' >>>…
MattL
  • 61
  • 1
1
2
3
26 27