Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am using Privoxy 3.0.10.0 to filter web pages before they're passed on to the browser.

I can't figure out why this simple regex doesn't trigger a rewrite. Maybe someone more experienced will have an idea:

Here's what it looks like when I hit Firefox's CTRL-U to view the HTML source:

<font color=#FF4AFF>JohnDoe</font>

Here's my regex; I've also added the "i" switch to ignore case, to no avail

s|(<font color=.+?>JohnDoe</font>)|<span class=myclass>$1</span>|g

Thanks for any hint.

share|improve this question

migrated from stackoverflow.com Jul 25 '09 at 9:08

This question came from our site for professional and enthusiast programmers.

1  
Shouldn't this be moved to serverfault? –  chakrit Jul 25 '09 at 9:42
1  
Given RichieHindle's answer: are you sure you want to embed the full original within <span> tags, or did you expect only the JohnDoe part to be copied, like <span class=myclass>JohnDoe</span>? Just to make sure that you're not overlooking <span class=myclass><font color=#FF4AFF>JohnDoe</font></span> in the result. –  Arjan Jul 25 '09 at 10:31

3 Answers 3

Thansk guys. Turns out Privoxy was greedy, and I didn't notice that it was taking much more data than I thought.

share|improve this answer

The regex itself works fine, as this Python example shows:

import re
print re.sub(r"(<font color=.+?>JohnDoe</font>)",
             r"<span class=myclass>\1</span>",
             "<font color=#FF4AFF>JohnDoe</font>")
# Prints <span class=myclass><font color=#FF4AFF>JohnDoe</font></span>

(assuming Privoxy uses the same regex syntax, barring the \1 vs. $1 difference, but it looks like it does.)

I guess the problem lies elsewhere - try a regex that can't fail, like replacing a with b, to see whether it's having any effect at all.

share|improve this answer

Not sure what RE engine you're using, but try changing the $1 to \1 - that's how backreferences are usually referred to in perl, at least.

share|improve this answer
    
Sometimes you'll need to use \\1, depending on in what format you're writing the regex. –  jtbandes Jul 25 '09 at 16:58
1  
In Apache's URL rewriting, one would use $1. But, if \1 should indeed be used in this case, then I'd expect the output to be <span class=myclass>$1</span> -- which hopefully would have been noted in the question then... –  Arjan Jul 25 '09 at 18:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.