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:

357 questions
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
  • 348,637
  • 121
  • 546
  • 611
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
1 answer

Negative lookahead with capturing groups

I'm attempting this challenge: https://regex.alf.nu/4 I want to match all strings that don't contain an ABBA pattern. Match: aesthophysiology amphimictical baruria calomorphic Don't…
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,…
Pharaoh
  • 3,329
  • 4
  • 21
  • 43
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
  • 36,468
  • 57
  • 89
  • 143
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,332
  • 1
  • 13
  • 32
8
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
  • 16,661
  • 9
  • 66
  • 120
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
  • 624
  • 6
  • 12
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
  • 2,473
  • 20
  • 39
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
2 answers

Is there an equivalent of "&" in R's regular expressions for backreference to entire match?

When I use vim, I often use & to backreference the entire match within substitutions. For example, the following replaces all instances of "foo" with "foobar": %s/foo/&bar/g The benefit here is laziness: I don't have to type the parenthesis in the…
crazybilly
  • 2,652
  • 13
  • 38
6
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
  • 914
  • 2
  • 10
  • 25
6
votes
2 answers

Are regular expressions (regex) really regular?

I understand how regular expressions got their name, and have read the related question (Why are regular expressions called "regular" expressions?), but am still wondering whether regular expressions are always regular. For example, how can…
6
votes
6 answers

What's the best way to clear regex matching variables?

What's the best way to clear/reset all regex matching variables? Example how $1 isn't reset between regex operations and uses the most recent match: $_="this is the man that made the new year rumble"; / (is) /; / (isnt) /; say $1; #…
vol7ron
  • 35,981
  • 19
  • 104
  • 164
5
votes
1 answer

How to replace exact number of characters in string based on occurrence between delimitors in R

I have text strings like this: u <- "she goes ~Wha::?~ and he's like ~↑Yeah believe me!~ and she's etc." What I'd like to do is replace all characters occurring between pairs of ~ delimitors (including the delimitors themselves) by, say, X. This…
Chris Ruehlemann
  • 10,258
  • 2
  • 9
  • 18
1
2
3
23 24