Page 1 of 1

Non greedy regular expression

Posted: Tue Feb 19, 2002 12:22 am
by Brimba
Hi!

If I have this:

<FONT COLOR="#cccccc">alot of text</FONT> alot of text <FONT COLOR="#cccccc">more text</FONT>

And this pattern:

<FONT COLOR="#cccccc">(.*?)<\/FONT>

And replace it with:

[hub]\1[/hub]

Then it would end up like this:

[hub]alot of text</FONT> alot of text <FONT COLOR="#cccccc">more text[/hub]

I want it to be:

[hub]alot of text[/hub] alot of text [hub]more text[/hub]

Is it possible?
Im running textPad version 4.5.0

Help!

///Brimba

Re: Non greedy regular expression

Posted: Tue Feb 19, 2002 8:25 am
by Jens Hollmann
If there are no other HTML-tags between "<FONT...>" and </FONT> you could search for:

<FONT COLOR="#cccccc">\([^<]*\)</FONT>

and replace with "[hub]\1[/hub]".

"[^<]*" matches anything except a "<" which makes the regexp in a way non-greedy. But there mustn't be any other tags ("<" to be precise) between the FONT tags.

The other possibility is to record a macro. Search for the start tag "<FONT COLOR="#cccccc">" then search for the end tag "</FONT>" expanding the selection and then do your replace in the current selection.

HTH

Jens