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
171
votes
17 answers

Regular expression for duplicate words

I'm a regular expression newbie and I can't quite figure out how to write a single regular expression that would "match" any duplicate consecutive words such as: Paris in the the spring. Not that that is related. Why are you laughing? Are my my…
Joshua
  • 6,643
  • 15
  • 55
  • 76
101
votes
5 answers

JavaScript - string regex backreferences

You can backreference like this in JavaScript: var str = "123 $test 123"; str = str.replace(/(\$)([a-z]+)/gi, "$2"); This would (quite silly) replace "$test" with "test". But imagine I'd like to pass the resulting string of $2 into a function,…
quano
  • 18,812
  • 25
  • 97
  • 108
56
votes
6 answers

Negating a backreference in Regular Expressions

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 want to match the string between the quotation…
Yuval A.
  • 5,849
  • 11
  • 51
  • 63
50
votes
2 answers

Backreferences Syntax in Replacement Strings (Why Dollar Sign?)

In Java, and it seems in a few other languages, backreferences in the pattern are preceded by a backslash (e.g. \1, \2, \3, etc), but in a replacement string they preceded by a dollar sign (e.g. $1, $2, $3, and also $0). Here's a snippet to…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
43
votes
1 answer

Notepad++ Regex Backreference syntax in Search/Replace - \1 or $1

I have tried to use the Notepad++ Search/Replace with a Regular Expression to replace specific words with shorter versions of those words. I used the following regex to match every word that ends with er (but not er as a word) - and replace the…
amiregelz
  • 1,833
  • 7
  • 25
  • 46
33
votes
4 answers

R: lookaround within lookaround

I need to match any 'r' that is preceded by two different vowels. For example, 'our' or 'pear' would be matching but 'bar' or 'aar' wouldn't. I did manage to match for the two different vowels, but I still can't make that the condition (...) of…
dasf
  • 1,035
  • 9
  • 16
29
votes
3 answers

How to backreference in Ruby regular expression (regex) with gsub when I use grouping?

I would like to patch some text data extracted from web pages. sample: t="First sentence. Second sentence.Third sentence." There is no space after the point at the end of the second sentence. This sign me that the 3rd sentence was in a separate…
Konstantin
  • 2,983
  • 3
  • 33
  • 55
21
votes
4 answers

How do backreferences in regexes make backtracking required?

I read http://swtch.com/~rsc/regexp/regexp1.html and in it the author says that in order to have backreferences in regexs, one needs backtracking when matching, and that makes the worst-case complexity exponential. But I don't see exactly why…
19
votes
5 answers

Circumvent the sed backreference limit \1 through \9

The sed manual clearly states that the available backreferences available for the replacement string in a substitute are numbered \1 through \9. I'm trying to parse a log file that has 10 fields. I have the regex formed for it but the tenth match…
Steve M
  • 335
  • 2
  • 4
  • 12
19
votes
1 answer

General approach for (equivalent of) "backreferences within character class"?

In Perl regexes, expressions like \1, \2, etc. are usually interpreted as "backreferences" to previously captured groups, but not so when the \1, \2, etc. appear within a character class. In the latter case, the \ is treated as an escape character…
kjo
  • 33,683
  • 52
  • 148
  • 265
18
votes
1 answer

%N backreference inside RewriteCond

I'm working on a virtual domain system. I have a wildcard DNS set up as *.loc, and I'm trying to work on my .htaccess file. The following code works: RewriteEngine On RewriteCond %{HTTP_HOST} ^(www.)?example\.loc$ [NC] RewriteCond %{REQUEST_URI}…
17
votes
5 answers

Python Regular Expressions to implement string unescaping

I am trying to implement string unescaping with Python regex and backreferences, and it doesn't seem to want to work very well. I'm sure it's something I'm doing wrong but I can't figure out what... >>> import re >>> mystring = r"This is \n a test…
eplawless
  • 4,225
  • 7
  • 34
  • 35
16
votes
2 answers

Python regex substitution: separate backreference from digit

In a regex replacement pattern, a backreference looks like \1. If you want to include a digit after that backreference, this will fail because the digit is considered to be part of the backreference number: # replace all twin digits by zeroes, but…
florisla
  • 12,668
  • 6
  • 40
  • 47
15
votes
5 answers

How to match a regex with backreference in Go?

I need to match a regex that uses backreferences (e.g. \1) in my Go code. That's not so easy because in Go, the official regexp package uses the RE2 engine, one that have chosen to not support backreferences (and some other lesser-known features) so…
Eldritch Conundrum
  • 8,452
  • 6
  • 42
  • 50
14
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…
1
2 3
26 27