Regex in 2 passes or a macro or two?

Find, replace, find in files, replace in files, regular expressions

Regex in 2 passes or a macro or two?

Postby Peter_LT » Tue Jan 09, 2007 6:12 am

Hello Folks,

I have about 2,500 occurrences of code which fall into two distinct types.

Example A
Code: Select all
<a href="image/mythicala.jpg" title="View image in new window"
onclick="newpop(this.href,'rel','415','570','0'); return false;">28 July Cabin Luncheon
<img src="image/mythicalz.jpg" width="85" height="120" alt="28 July 1950 Cabin Luncheon Menu cover"
/></a>

Example B
Code: Select all
<a href="img/tktenv1950a.jpg" title="View image in new window"
onclick="newpop(this.href,'rel','570','350','0'); return false;">
<img src="img/tktenv1950az.jpg" width="195" height="116" alt="28 July train ticket envelope"
/><br />
Train Ticket Envelope</a>

The distinct difference between the two is that a <br/ > occurs only in Example B.

I'm attempting to place a small graphic to show that the links open a new window, using two CSS classes. In Example A the first line will become...
Code: Select all
<a class="pu" href="image/mythicala.jpg" title="View image in new window"


Example B will have a span added to the bottom line viz...
Code: Select all
<span class="ext">Train Ticket Envelope</span></a>


Could this be done with regex in 2 passes, bearing in mind that the start text of...
"a href" with "in new window" will be changed to
"a class="ext" href" with "in new window"
for no occurrence of <br /> before closing </a> in a first pass.

I have read and re-read regex help, both in UE and in Topstyle and I'm still very confused, despite several hours of trial and error...
...and as for macros - I'd not know where to begin.

Is my aim too ambitious? This is more or less the last step in my conversion from HTML 3.2 to accessible XHTML1.0 with CSS. I'm dreading the idea of having to change each entry manually one by one.

I've done one page to show <an example of the desired outcome>.

Any advice you could give would be very gratefully received - thanks.

Regards,
Steve (Peter S.)
Caronia II Timeline Webmaster

PS: I've been using UE since V4 - now 12.20
User avatar
Peter_LT
Newbie
 
Posts: 3
Joined: Tue Jan 09, 2007 12:00 am

Re: Regex in 2 passes or a macro or two?

Postby Mofi » Tue Jan 09, 2007 8:34 am

Regular expression in UltraEdit style for example A:

Find: ^(<a href="*"^) ^(title="View image in new window"[~>]+>[~<]+<img src=[~>]+></a>^)
Replace: ^1 class="pu" ^2

Regular expression in UltraEdit style for example B:

Find: ^(<a href="*" title="View image in new window"[~>]+>[~<]+<img src=[~>]+><br />[ ^t^r^n]+^)^(*^)</a>
Replace: ^1<span class="ext">^2</span></a>
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4051
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Regex in 2 passes or a macro or two?

Postby Peter_LT » Wed Jan 10, 2007 2:44 am

Hello Mofi,

Job done!

What can I say, but thank you so much for this. I don't have much hair and this problem was causing me to lose what little I had.

You clearly have this arcane language of Regex at your fingertips. I've got years of dBase IV programming and HTML writing under my belt, but this for some reason Regex still defeats me.

If I can offer a suggestion - it would be good to have a Tutorial Section here, and you'd be a key man in writing the Regex one.

Thanks again!
Regards,
Steve (Peter S.)
Caronia II Timeline Webmaster
User avatar
Peter_LT
Newbie
 
Posts: 3
Joined: Tue Jan 09, 2007 12:00 am

Re: Regex in 2 passes or a macro or two?

Postby mrainey56 » Wed Jan 10, 2007 2:52 am

There are tons of regex tutorials available out there, but only one Mofi right here. Let's save him for the UE specific hair-pullers. :)

http://www.regular-expressions.info/
User avatar
mrainey56
Master
Master
 
Posts: 212
Joined: Tue Jul 27, 2004 11:00 pm
Location: Spartanburg, South Carolina

Re: Regex in 2 passes or a macro or two?

Postby Peter_LT » Wed Jan 10, 2007 4:04 am

mrainey56 wrote: Let's save him for the UE specific hair-pullers. :)

Oh, in that case - absolutely!

Regards
Steve (Peter S.)
User avatar
Peter_LT
Newbie
 
Posts: 3
Joined: Tue Jan 09, 2007 12:00 am


Return to Find/Replace/Regular Expressions