Multiline Regexps

Either this is a bug, or I’m missing something:

irb(main):001:0> /^[aeiou]/.match(“foo\noooh”)
=> #MatchData:0x81d588c
irb(main):002:0> /^[aeiou]/m.match(“foo\noooh”)
=> #MatchData:0x81d2cf4

Shouldn’t the second one return nil?

ruby 1.8.1 (2003-12-03) [i386-netbsdelf]

Thanks.

···


Ceri Storey cez@necrofish.org.uk

Don’t mind me, I’m an idiot–I wanted ‘\A’ instead of ‘^’.

(Thanks Jason)

···

On Wed, Dec 10, 2003 at 01:56:43AM +0900, Ceri Storey wrote:

Shouldn’t the second one return nil?


Ceri Storey cez@necrofish.org.uk

I believe that Ruby’s version of /m just makes . match \n (which it normally
doesn’t); the ^ and $ always match around \n. If you want to match the
beginning or end of the entire string use \A instead of ^ and \Z instead of
$.

-Mark

···

On Wed, Dec 10, 2003 at 01:56:43AM +0900, Ceri Storey wrote:

Either this is a bug, or I’m missing something:

irb(main):001:0> /[1]/.match(“foo\noooh”)
=> #MatchData:0x81d588c
irb(main):002:0> /[2]/m.match(“foo\noooh”)
=> #MatchData:0x81d2cf4

Shouldn’t the second one return nil?


  1. aeiou ↩︎

  2. aeiou ↩︎

You’re not an idiot. :slight_smile: The /m modifier in Perl does do exactly
the inverse of what you thought it did in Ruby - make ^ and $
match around \n’s (in Perl they default to only the beginning and
end of the string). The various flavors of regular expression
floating around can be confusing, and a lot of Rubyists
start out in Perl, which doesn’t help in this instance. :slight_smile:

The Perl->Ruby mapping looks like this:

Perl = Ruby Meaning
(no modifiers) (not supported) ^/$ at ends of str
/m (no modifiers) ^/$ around \n
/ms /m . matches \n

Then there’s Ruby’s /s, which doesn’t affect . or ^/$, but adds support
for matching multibyte characters (which you might expect to be /m, but
I think /s stands for SJIS).

-Mark

···

On Wed, Dec 10, 2003 at 02:03:41AM +0900, Ceri Storey wrote:

On Wed, Dec 10, 2003 at 01:56:43AM +0900, Ceri Storey wrote:

Shouldn’t the second one return nil?

Don’t mind me, I’m an idiot–I wanted ‘\A’ instead of ‘^’.