backreference

Python Regular Expressions

I'm trying to implement string unescaping with Python regex and backreferences, and it doesn't seem to want to work very well. I'm sure it's something I'm doing wrong but I can't figure out what... >>> import re >>> mystring = r"This is \n a test \r" >>> p = re.compile( "\\\\(\\S)" ) >>> p.sub( "\\1", mystring ) 'This is n a test r' >>>...

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 longer in preprocessor. The question is how I can match the explicit line continuation ...

Python: \number Backreference in re.sub

I'm trying to use python's re.sub function to replace some text. >>> import re >>> text = "<hi type=\"italic\"> the></hi>" >>> pat_error = re.compile(">(\s*\w*)*>") >>> pat_error.search(text) <_sre.SRE_Match object at 0xb7a3fea0> >>> re.sub(pat_error, ">\1", text) '<hi type="italic">\x01</hi>' Afterwards the value of text should be ...

RewriteCond backreference not working

If I go to http://www.example.com I want it to stay there, which is working fine. If I go to http://bar.example.com it redirects to http://www..com, which is wrong I want it to go to http://www.example.com given the backreference in the RewriteCond RewriteEngine On RewriteCond %{HTTP_HOST} !^$ RewriteCond %{HTTP_HOST} !^www.(exa...

if match(g:possibilities, "using backreferences after the regex in vim\(script\)?")

I'd like to be able to, say, check the value of $1 inside the if block, to get the first backreference. However vimscript is not perl. Is there some way to do this? I'm aware of the possibility of using \1, \2, etc., inside the regex, but I'd like to match and then use the values in subsequent statements, as is possible in perl, php, ...

RegEx Backreferences

Having the following regular expression: ([a-z])([0-9])\1 It matches a5a, is there any way for it to also match a5b, a5c, a5d and so on? EDIT: Okay, I understand that I could just use ([a-z])([0-9])([a-z]) but I've a very long and complicated regular expression (matching sub-sub-sub-...-domains or matching an IPv4 address) that wou...

Regex: recursive backreferences - what for?

I found some interesting possibility in many regex engines: It's possible to place backreference inside the capture group and reference this group. For example: (\1) My question: for what regex patterns it may be used? I can't imagine... ...

javascript regexp backreferences in character class possible?

Hi, 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 does: htmlString.replace(/attribute_name=(['"])[^'"]*\1/,'') Unfortunatelly my attribute_name can contain apostrophes or quote...

Search and Replace using Backrefrence in vbscript for excel 2007

Hi, 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 = Pattern r.IgnoreCase = IgnoreCase r.Global = True S = r.Replace(Value, R...

Are back references possible in flex (lexical analyser)?

Hi, 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 the use of back references in flex possible? If so, how can I do it? I can't find any...

JavaScript - string regex backreferences

You can backreference like this in JavaScript: var str = "123 $test 123"; str = str.replace(/(\$)([a-z]+)/gi, "$2"); This would (quite silly) replace "$test" with "test". But imagine I'd like to pass the resulting string of $2 into a function, which returns another value. I tried doing this, but instead of getting the string "test", I...

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 RE("^([0-9]{3}.[0-9]) \1\r$"); System.out.println(re.match(str)); The back references do...

Regular Expression: back references

sed 's/^\(\h*\)\(.*\)$/\1<!-- \2 -->/' web.xml I think that this should take this xml: <a> <d> bla </d> </a> And turn it into: <!-- <a> --> <!-- <d> --> <!-- bla --> <!-- </d> --> <!-- </a> --> But what is doing is this: <!-- <a> --> <!-- <d> --> <!-- bla --> <!-- </d> --> <!-- </a> --> ...

javascript regex: match altered version of first match with only one expression

Hi there I'm writing a brush for Alex Gorbatchev's Syntax Highlighter to get highlighting for Smalltalk code. Now, consider the following Smalltalk code: aCollection do: [ :each | each shout ] I want to find the block argument ":each" and then match "each" every time it occurrs afterwards (for simplicity, let's say every occurrence a...

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

Hi, 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, when I enter the URL: http://example.com/index.php?route=product/category&amp;path=3...

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 some quoted. Say I want it to also support single quotes, I could change the expressio...

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 use it? For example with the use of "([0-9]{1})" in the "Find what" i will find all the...

Backreferences in lookbehind

Can you use backreferences in a lookbehind? Let's say I want to split wherever behind me a character is repeated twice. String REGEX1 = "(?<=(.)\\1)"; // DOESN'T WORK! String REGEX2 = "(?<=(?=(.)\\1)..)"; // WORKS! System.out.println(java.util.Arrays.toString( "Bazooka killed the poor aardvark (yummy!)" .sp...

ColdFusion - pass regex backreference to function call

Hi, 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: <cfset s = "STARTDATE_2010-05-07 00:05:00.0_ENDDATE" /> <cfset s = reReplace(s, "STARTDATE_([\s-.:0-9]*)_ENDDATE", dateAd...

Backreferences syntax in replacement strings (why dollar sign?)

In Java, and it seems in a few other languages, backreferences in the pattern is preceded by a slash (e.g. \1, \2, \3, etc), but in a replacement string it's preceded by a dollar sign (e.g. $1, $2, $3, and also $0). Here's a snippet to illustrate: System.out.println( "left-right".replaceAll("(.*)-(.*)", "\\2-\\1") // WRONG!!! ); //...