I’m doing some html replacement using regular
expressions and i ran into this issue…
Example reg example => /[b](.*)[/b]/
…works fine to match bold please 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,
···
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
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 to the last ,
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 bold
irb(main):002:0" text, and here is some more
irb(main):003:0" bold text”
=> “Here is some bold\ntext, and here is some more\nbold text”
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, ‘\1’/)
=> “Here is some bold\ntext[/b], and here is [b]some more\nbold text”
irb(main):007:0> x.gsub(%r{[b](.?)[/b]}m, ‘\1’/)
=> “Here is some bold\ntext, and here is some more\nbold text”
-Mark
···
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 bold please but NOT
with [b]bold
please[/b]