From: "Mark J. Reed" Date: 2003-09-24T09:56:51+09:00 Subject: Re: regular expression 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, '\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