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
96
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
  • 17,702
  • 24
  • 94
  • 108
50
votes
5 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,007
  • 9
  • 47
  • 59
48
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
  • 348,637
  • 121
  • 546
  • 611
40
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,718
  • 7
  • 20
  • 36
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
  • 995
  • 7
  • 12
28
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,575
  • 2
  • 26
  • 47
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…
18
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
  • 325
  • 1
  • 4
  • 12
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}…
16
votes
2 answers

Python regex subsitution: 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
  • 8,866
  • 4
  • 31
  • 41
15
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,027
  • 6
  • 30
  • 35
14
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
  • 27,601
  • 42
  • 124
  • 225
13
votes
4 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
  • 7,892
  • 6
  • 42
  • 48
13
votes
3 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
  • 11,809
  • 7
  • 42
  • 67
12
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
  • 18,516
  • 18
  • 99
  • 168
1
2 3
23 24