Reg Ex Find and Replace help

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
burtonfigg
Posts: 11
Joined: Wed Jun 01, 2005 6:37 pm

Reg Ex Find and Replace help

Post by burtonfigg »

Hi,

I have a big long web page, with lots of web addresses in - e.g:

<a href="http://www.google.co.uk/search?q=mysql+ ... -8">Google for MySQL Tutorials</a>

Would it be possible to do a find and replace, using a reg ex which finds all ampersands contained within the hyperlinks in any <a href> tags, and replace them with "&amp;"?

Thanks

Jim
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Tricky. I don't believe you can do that in TextPad in a single step, or even in a single repeated step.

But you can do it in WildEdit:
Find what: &(?!amp;)(?=[^<]*>)
Replace with: &amp;

[X] Regular expression
Boris
Posts: 16
Joined: Mon Apr 24, 2006 1:46 pm
Location: UK

Post by Boris »

How about this multi-step solution:
(as ever, use POSIX regular expression syntax)

Search : (<a href.*)\x26(.*>)
Replace with: \1###amp;\2

[X] Regular expression

Repeatedly using "Replace All" will substitute all ampersands (Hex 26), within an <a href > line, for "###amp;".

When no more matches are found, then:

Search : ###
Replace with: &

[ ] Regular expression

Hope this helped.
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

(<a href.*)\x26(.*>) or, more simply, (<a href.*)&(.*>) does not match just <a> tags containing ampersands. It matches everything from an <a href to the last > on the same line, provided there's an ampersand in between. So it will replace ampersands within the bodies of single-line <a> elements and between <a> elements on the same line as well as ampersands in <a> tags. This is not what the original poster asked for.

You might try changing .* into [^<>]*
Post Reply