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

Use regex backreference in calculation(Java,Netbeans and not only)

In Netbeans the "Replace command"(ctrl+H) has a regular expression checkbox so that i can search and replace in the document using regex's. Also i can replace using a backreference. The question is can i use a backreference in a calculation and then…
Argiropoulos Stavros
  • 9,436
  • 11
  • 61
  • 79
0
votes
1 answer

Surround HTML open and close tag pair with comments

I need to write a java regex substitution (preferably purely declarative) that will do these analogous transformations: 1) Some visible text to Some visible…
Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
0
votes
0 answers

How to match appropriate braces (with regular expressions) in Java

I'd like to match parts in a string that are enclosed by braces ({}), and refer back to the content within the braces. The content within the braces can also contain "nested" braces, which makes it important to match the correct closing braces. So,…
Edward
  • 4,453
  • 8
  • 44
  • 82
0
votes
4 answers

Is there a regex engine that supports "for each captured group" in replacement strings?

Here's my example. If I want to use a regex to replace tabs in the code with spaces, but wanted to preserve tab characters in the middle or end of a line of code, I would use this as my search string to capture each tab character at the start of a…
brokethebuildagain
  • 2,162
  • 1
  • 22
  • 44
0
votes
1 answer

Jakarta Regexp 1.5 Backreferences?

Why does this match: String str = "099.9 102.2" + (char) 0x0D; RE re = new RE("^([0-9]{3}.[0-9]) ([0-9]{3}.[0-9])\r$"); System.out.println(re.match(str)); But this does not: String str = "099.9 102.2" + (char) 0x0D; RE re = new…
msi
  • 201
  • 2
  • 6
0
votes
1 answer

Backreferences to capturing group instead of captured result

I want to use regular expression to check whether a given string is an IP address or not. My first idea was ^([0-2][0-9]{1,2}|[0-9]{1,2})\.\1\.\1\.\1$, but then I remembered that backreferences are referencing to the result of the capturing group.…
Cubi73
  • 1,891
  • 3
  • 31
  • 52
0
votes
1 answer

regex for parsing a img tag in html with parameters

I have this notational language for posting stuff on my blog and I use the following line to parse images $$link_to_image width height alternative_description$$ I am parsing this with the following php sentence…
Aditya
  • 1,240
  • 2
  • 14
  • 38
0
votes
0 answers

Replace all backreferences in regex

When trying to replace the cout's I had used for debugging for a more elaborate logging function. I ran into the following problem: A message currently is constructed like this: testValue; cout << "value" << testValue; And should become…
laurisvr
  • 2,724
  • 6
  • 25
  • 44
0
votes
2 answers

bug on module re in python (backreference)?

I want to match: first second and second first so the regular expression: re.match(r'(?:(?Pfirst) (?Psecond)|(?P=s) (?P=f))', 'first second') matches, but this one: re.match(r'(?:(?Pfirst) (?Psecond)|(?P=s) (?P=f))', 'second…
Matteo
  • 65
  • 1
  • 6
0
votes
1 answer

Search and Replace using Backrefrence in vbscript for excel 2007

I'm using excel 2007 and i'm adding a macro that looks something like this : Function S(Value As String, Pattern As String, ReplaceWith As String, Optional IgnoreCase As Boolean = False) Dim r As New VBScript_RegExp_55.RegExp r.Pattern =…
DMin
  • 10,049
  • 10
  • 45
  • 65
0
votes
2 answers

javascript regexp backreferences in character class possible?

Does javascript regular expressions support backreferences inside character class? I want to do something like this: htmlString.replace(/attribute_name=(['"])[^\1]*\1/,'') But that does not work. This…
SWilk
  • 3,261
  • 8
  • 30
  • 51
0
votes
2 answers

Java named backreferences not matching

I'm writing a simplified SQL parser that's using regexes to match each valid command. I'm stuck on matching the following: attribute1 type1, attribute2 type2, attribute3 type3, ... Where attributes are names of table columns, and types can be a…
John V
  • 269
  • 3
  • 15
0
votes
1 answer

How to search for a negated Unicode backreference (PCRE)?

I'd like to find two non-identical Unicode words separated by a colon using a PCRE regex. Take for example, this string: Lôrem:ipsüm dõlör:sït amêt:amêt cønsectetûr:cønsectetûr âdipiscing:elït I can easily find the two identical words separated by…
Nemo XXX
  • 644
  • 2
  • 14
  • 35
0
votes
1 answer

c++11 regex back references

I don't manage to use back references in regular expression in c++. After trying more esoteric things, I tried this simple script on gcc 4.8.1: #include #include #include using namespace std; int main() { regex…
UndefinedBehavior
  • 836
  • 1
  • 11
  • 20
0
votes
1 answer

How to pass a replacing regex with a backreference as a command line argument to a Perl script

Previous post related: How to pass a replacing regex as a command line argument to Perl After reading the above I wrote a very similar script, but I can't get it to interpolate as I intend when I use backreferences such as $1. It could be that I'm…