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

makefile backreference to matched text

Suppose, I have 2 files, dependent upon each other: ./pictures/1_data.tex | V ./data/1.pl So, 1_data.tex is generated from the Perl file. To do it I have the following rule in the makefile: ./pictures/1_data.tex:…
user4035
  • 22,508
  • 11
  • 59
  • 94
0
votes
3 answers

Parsing of a string with the length specified within the string

Example data: 029Extract this specific string. Do not capture anything else. In the example above, I would like to capture the first n characters immediately after the 3 digit entry which defines the value of n. I.E. the 29 characters "Extract…
Tim Radcliffe
  • 1,883
  • 2
  • 20
  • 29
0
votes
3 answers

Vim regex, backreference to `[^s]` matches

In a file with something like: self.phrase self.phrases self.phrase.lower() self.phrase_lowered self.phrases[self.phrase] I'd like to replace all self.phrase with self._phrase, except for self.phrases to…
Kache
  • 15,647
  • 12
  • 51
  • 79
0
votes
3 answers

Accessing $1 etc. outside of string.replace() in javascript regex?

As we all know $1 and so on are backreferences to captured groups in a string.replace() when using a regex, so you can do something like: string.replace(/(http:\/\/\S*)/g, 'link<\/a>') Now my question is whether there…
Wingblade
  • 9,585
  • 10
  • 35
  • 48
0
votes
1 answer

How do I use Find/Replace with a backreference followed by a number?

I want to do a find/replace on the string matching (abcdefg)1 with \12, but it doesn't work. How do I do it?
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
0
votes
1 answer

How do I update a backreference inline in JavaScript?

Given the following code: alpha = ('"foo"="bar"').replace(/.*foo...([^"]*).*/, RegExp.$1) beta = ('"bar"="baz"').replace(/.*bar...([^"]*).*/, RegExp.$1) The expected output is: alpha is "bar" beta is "bar" The actual output is: alpha is "" beta is…
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
0
votes
1 answer

Regular expressions with back references

So I have this really long file in which there are lines that are built like this: `somecode [ somecode > somecode ] somecode > somecode > somecode` I have to search for a string of atleast 9 + or - characters between the brackets and the same…
Pieterjan
  • 445
  • 6
  • 23
0
votes
2 answers

php preg_match_all backreference

for the following input string , pattern and : $str1 = 'span class="outline">Iron Man butts heads with Nick Fury and Shield after HYDRA attacks a meeting of the United Nations. Dir: Vinton…
aditya parikh
  • 595
  • 4
  • 11
  • 30
0
votes
2 answers

Optimization of multiple expressions to one

BACKGROUND I have a scenario where I must repeatedly find certain words in text, over and over. I have currently used a series of regular Expressions in a format like…
DarrenMB
  • 2,342
  • 1
  • 21
  • 26
0
votes
2 answers

Python: \number Backreference in re.sub

I'm trying to use python's re.sub function to replace some text. >>> import re >>> text = " the>" >>> pat_error = re.compile(">(\s*\w*)*>") >>> pat_error.search(text) <_sre.SRE_Match object at 0xb7a3fea0> >>>…
Daniel
  • 25
  • 1
  • 7
0
votes
3 answers

strange behavior of parenthesis in python regex

I'm writing a python regex that looks through a text document for quoted strings (quotes of airline pilots recorded from blackboxes). I started by trying to write a regex with the following rules: Return what is between quotes. if it opens with…
James M. Lay
  • 2,270
  • 25
  • 33
0
votes
1 answer

Back-references in JavaScript regular expressions

Is there anyway to pick up back-references? var name = "HELLO WORLD" var patt = /\S+\s(.+)/; alert(name.match(patt)); This is just a simple example to get every word after the first. But, if I alert $1, nothing pops up and I'm not sure why. I'd…
user1464055
  • 603
  • 2
  • 9
  • 19
0
votes
3 answers

How do I ignore $1 replace backreferencing in javascript

I have a string that a user can edit at any time, and a regex that is being conducted on the string, to add it to an xml and then save it but they can add '$1' to the string. I just want the text '$1' to be saved but I have to perform a regular…
WPAflight
  • 75
  • 1
  • 10
0
votes
4 answers

sed 's/.../...': Is it possible to store subexpressions for later use?

supposing I have something like that: echo "bLah BLaH blAH" | sed -r 's/([a-zA-Z ]+)/\L&; s/[a-z]/\u&/g' Quite a typical use for sed to get a "crazy-case" string into mixed case (first letter uppercase, rest of letters lowercase) However, this will…
syntaxerror
  • 661
  • 2
  • 6
  • 24
0
votes
2 answers

Python Regular Expression: BackReference

Here is the Python 2.5 code (which replace the word fox with a linkfox, and it avoided the replacement inside a link): import re content="""

The quick brown fox jumped…

Susan Mayer
  • 335
  • 1
  • 3
  • 12