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
6
votes
2 answers

Why not create a backreference?

I understand that putting ?: inside of the start of the parentheses of a regular expression will prevent it from creating a backreference, which is supposed to be faster. My question is, why do this? Is the speed increase noticeable enough to…
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
6
votes
0 answers

Why regular expressions with backreferences are not regular expressions?

This article say: A backreference like \1 or \2 matches the string matched by a previous parenthesized expression, and only that string: (cat|dog)\1 matches catcat and dogdog but not catdog nor dogcat. As far as the theoretical term is…
macabeus
  • 4,156
  • 5
  • 37
  • 66
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,992
  • 1
  • 16
  • 42
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
2 answers

How do I use more than nine backreferences in Notepad++ regexp?

If I use a long regular expression in Notepad++, i.e.: ^([^ ]+) ([^ ]+) ([^ ]+) (\[.*?\]) (".*?") (".*?") (".*?") (".*?") (\d+) (\d+) (\d+)$ (this is for turning an Apache log lines from space-separated to tab-separated) then I can't successfully…
watery
  • 5,026
  • 9
  • 52
  • 92
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
  • 40,809
  • 21
  • 119
  • 172
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
  • 20,321
  • 4
  • 12
  • 34
5
votes
5 answers

Match same number of repetitions of character as repetitions of captured group

I would like to clean some input that was logged from my keyboard with python and regex. Especially when backspace was used to fix a mistake. Example 1: [in]: 'Helloo world' [out]: 'Hello world' This can be done with re.sub(r'.', '',…
Louis M
  • 4,036
  • 4
  • 21
  • 25
5
votes
1 answer

Negative backreferences in MySQL REGEXP

MySQL manual is not very detailed about what expressions it supports, so I am not sure if the following is possible with MySQL at all. I am trying to create a query with RLIKE which matches the following. The task is to get from SQL all the…
JustAMartin
  • 13,165
  • 18
  • 99
  • 183
5
votes
1 answer

How to use back reference with stringi package?

In R I can use \\1 to reference to a capturing group. However, when using the stringi package, this doesn't work as expected. library(stringi) fileName <- "hello-you.lst" (fileName <- stri_replace_first_regex(fileName, "(.*)\\.lst$", "\\1")) [1]…
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
5
votes
1 answer

Backslashes in gsub (escaping and backreferencing)

Consider the following snippet: puts 'hello'.gsub(/.+/, '\0 \\0 \\\0 \\\\0') This prints (as seen on ideone.com): hello hello \0 \0 This was very surprising, because I'd expect to see something like this instead: hello \0 \hello \\0 My argument…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
5
votes
4 answers

Can you use back references in the pattern part of a regular expression?

Is there a way to back reference in the regular expression pattern? Example input string: Here is "some quoted" text. Say I want to pull out the quoted text, I could create the following expression: "([^"]+)" This regular expression would match…
Camsoft
  • 11,718
  • 19
  • 83
  • 120
5
votes
2 answers

regex: getting backreference to number, adding to it

Simple regex question: I want to replace page numbers in a string with pagenumber + some number (say, 10). I figured I could capture a matched page number with a backreference, do an operation on it and use it as the replacement argument in…
ako
  • 3,569
  • 4
  • 27
  • 38
5
votes
4 answers

Applying a function to a backreference within gsub in R

I'm new to R and am stuck with backreferencing that doesn't seem to work. In: gsub("\\((\\d+)\\)", f("\\1"), string) It correctly grabs the number in between parentheses but doesn't apply the (correctly defined, working otherwise) function f to…
JMD
  • 63
  • 3
5
votes
5 answers

Perl Regex Multiple Matches

I'm looking for a regular expression that will behave as follows: input: "hello world." output: he, el, ll, lo, wo, or, rl, ld my idea was something along the lines of while($string =~ m/(([a-zA-Z])([a-zA-Z]))/g) { print "$1-$2 "; …
Johann
  • 273
  • 1
  • 6
  • 11
1 2
3
26 27