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
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
  • 22,871
  • 7
  • 116
  • 244
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
  • 22,871
  • 7
  • 116
  • 244
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
  • 385
  • 5
  • 22
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
  • 535
  • 3
  • 11
  • 27
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,248
  • 1
  • 18
  • 25
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
1 answer

Python Regex: escaping a back-reference

Here is my situation: re.sub(r'([^\\])', r'\1[\W\1]*', string) It is straight forward that I want to append [\W(itself)] after (itself) for itself being a group of characters (can be special). That is why I need to put it in a set to strip away all…
Squall Leohart
  • 535
  • 2
  • 5
  • 17
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
  • 1,606
  • 18
  • 26
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
  • 563
  • 2
  • 8
  • 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
  • 9
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
  • 551
  • 2
  • 4
  • 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
  • 317
  • 1
  • 3
  • 12
0
votes
1 answer

How do I return a portion of a string with ack (or an equivalent)?

I have a line in a file that looks like this: $db['foo']['database'] = 'bar'; I want to use ack or grep or something to return bar out of that string. So far I have: ack '^\$db\['\''foo'\''\]\['\''database'\''\] = '\''([\w_]+)'\' $file But don't…
futuraprime
  • 4,949
  • 6
  • 32
  • 46
0
votes
1 answer

How Many Parameters Allowed in VBA Regex .Replace Method?

! I am working with the replace method of VBA Regex and am wondering how many parameters it can take? I've seen the msdn page that gives lots of different options, but I'm unsure about which to use. Here's the link to that page: MSDN Page:…
buck1112
  • 504
  • 8
  • 23
-1
votes
3 answers

Why is my Back Reference in my Regular Expression Not Working?

I wrote a regular expression trying to match some html code but I can't quite it to work. I'm having a problem with the part after "wp-caption". class=(["\'])(?:[\w\s])*?wp-caption[\s\1] The code I want to…
BFTrick
  • 4,331
  • 5
  • 22
  • 28
1 2 3
23
24