[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

regular expression

paul vudmaska

9/24/2003 12:34:00 AM

I'm doing some html replacement using regular
expressions and i ran into this issue...

Example reg example => /\[b\](.*)\[\/b\]/

....works fine to match [b]bold please[/b] but NOT

with [b]bold

please[/b]

... where a new line is introduced so i tried this reg
exp...

/\[b\]([.\s]*)\[\/b\]/

and this...
/\[b\]([.\n]*)\[\/b\]/

but neither of the last 2 match at all.

What am i missing?

thanks,:P





__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder...

1 Answer

Mark J. Reed

9/24/2003 12:54:00 AM

0

On Wed, Sep 24, 2003 at 09:34:12AM +0900, paul vudmaska wrote:
> I''m doing some html replacement using regular
> expressions and i ran into this issue...
>
> Example reg example => /\[b\](.*)\[\/b\]/
>
> ...works fine to match [b]bold please[/b] but NOT
>
> with [b]bold
>
> please[/b]

That''s because ''.'' doesn''t match newline by default. To get it to,
you need to add the /m flag. (You also need to have more than the one
line in the string, of course).

However, * is greedy, which means that given this text:

Here is some [b]bold text[/b], and here is [b]some more bold text[/b]

Your regex will match all the way from the first [b] to the last [/b],
rather than just to the first [/b]. You can prevent that by using the
non-greedy qualifier ?, which goes after the *. Also, I would use %r
instead of /.../ just to avoid one of those nasty backslashes. :)

So:

irb(main):001:0> x = "Here is some [b]bold
irb(main):002:0" text[/b], and here is [b]some more
irb(main):003:0" bold text[/b]"
=> "Here is some [b]bold\ntext[/b], and here is [b]some more\nbold text[/b]"
irb(main):004:0> x =~ %r{\[b\](.*)\[/b\]}
=> nil
irb(main):005:0> x =~ %r{\[b\](.*)\[/b\]}m
=> 13
irb(main):006:0> x.gsub(%r{\[b\](.*)\[/b\]}m, ''<b>\1</b>''/)
=> "Here is some <b>bold\ntext[/b], and here is [b]some more\nbold text</b>"
irb(main):007:0> x.gsub(%r{\[b\](.*?)\[/b\]}m, ''<b>\1</b>''/)
=> "Here is some <b>bold\ntext</b>, and here is <b>some more\nbold text</b>"

-Mark