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
4
votes
1 answer

Match uneven number of escape symbols

I need to match C++ preprocessor statements. Now, preprocessor statements may span multiple lines: #define foobar \ "something glorious" This final backslash may be escaped so the following results in two separate lines: #define foobar \\ No…
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
4
votes
1 answer

Info about backtracking control verbs

I been searching info about this verbs, There is not much info about that, i only found info like this Wikipedia but is not about the verbs, they just mentioned. I googled it, but not so lucky, i just found examples from here(stack overflow)…
nEAnnam
  • 1,246
  • 2
  • 16
  • 22
4
votes
1 answer

Problem with backreferences in C#'s regex

The goal is to extract time and date strings from this: Date - Thursday, June 2 2011 9:00PM
Here's the code: Match m = Regex.Match(line, "Date - (.*) (.*)
"); date = m.Captures[0].Value; time =…
Hui
  • 13,887
  • 8
  • 25
  • 20
4
votes
1 answer

How can I backreference matches in a recursive regular expression?

I have a string like this: $data = 'id=1 username=foobar comment=This is a sample comment'; And I would like to remove the \n in the third field (comment=...). I have this regular expression that serves my purpose but not so…
elitalon
  • 9,191
  • 10
  • 50
  • 86
4
votes
2 answers

Use previous backreference as name of named capture group

Is there a way to use a backreference to a previous capture group as the name of a named capture group? This may not be possible, if not, then that is a valid answer. The following: $data = 'description: some description'; preg_match("/([^:]+):…
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
4
votes
2 answers

Named backreference (?P=name) issue in Python re

I am learning 're' part of Python, and the named pattern (?P=name) confused me, When I using re.sub() to make some exchange for digit and character, the patter '(?P=name)' doesn't work, but the pattern '\N' and '\g' still make sense. Code…
KE LI
  • 43
  • 3
4
votes
3 answers

Vim / sed regex backreference in search pattern

Vim help says that: \1 Matches the same string that was matched by */\1* *E65* the first sub-expression in \( and \). {not in Vi} Example: "\([a-z]\).\1" matches "ata", "ehe", "tot", etc. It looks like the backreference…
Dave Grabowski
  • 1,619
  • 10
  • 19
4
votes
2 answers

Python regex - Replace bracketed text with contents of brackets

I'm trying to write a Python function that replaces instances of text surrounded with curly braces with the contents of the braces, while leaving empty brace-pairs alone. For example: foo {} bar {baz} would become foo {} bar baz. The pattern that…
halbrd
  • 115
  • 1
  • 7
4
votes
1 answer

How to apply a function on a backreference?

Say I have strings like the following: old_string = "I love the number 3 so much" I would like to spot the integer numbers (in the example above, there is only one number, 3), and replace them with a value larger by 1, i.e., the desired result…
Yuhuan Jiang
  • 2,616
  • 2
  • 19
  • 21
4
votes
3 answers

ColdFusion - pass regex backreference to function call

I'm using ColdFusion's reReplace() function for regular expression pattern replacement. I'd like to use a function call for the replacement string, and pass a matched backreference to it. Something like this:
stubotnik
  • 1,952
  • 2
  • 17
  • 31
4
votes
1 answer

Problem with RewriteCond %{QUERY_STRING}, backreference not dispaying in final URL

I have the following in my .htaccess file: RewriteCond %{QUERY_STRING} ^route\=product\/category\&path\=35\&page\=([0-9]+)$ RewriteRule ^index\.php$ http://%{HTTP_HOST}/product/category/35/page_$1? [R=301,L] It's not behaving as expected though,…
eb_Dev
  • 873
  • 2
  • 13
  • 25
4
votes
4 answers

Matching incremental digits in numbers

After googling many days about the issue, finally I am posting this question here and hoping to get it solved by experts here; I am looking for the regex pattern that can match incremental back references. Let me explain: For number 9422512322, the…
Gadara
  • 73
  • 4
4
votes
2 answers

Are back references possible in flex (lexical analyser)?

I'm used to play with regexp in languages where I can use parenthesis to capture references. The only thing near that in flex that I'm seeing is the yytext variable. But it's contents are the full matched regexp and not just some part of it. Isn't…
rfgamaral
  • 16,546
  • 57
  • 163
  • 275
4
votes
1 answer

Do backreferences need to come after the group they reference?

While running some tests for this answer, I noticed the following unexpected behavior. This will remove all occurrences of after the first: var input = "extra"; Regex.Replace(input,…
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
4
votes
4 answers

Wrap first word in tag with preg_replace -- can't reference fullstring match

I generated the following regex code with http://gskinner.com/RegExr/ where it works, but when I execute it with PHP, it fails to use the match in the replacement string. preg_replace( '/(?<=\>)\b\w*\b|^\w*\b/', '$&', 'an…
John Doe Smith
  • 1,623
  • 4
  • 24
  • 39