Newline and regular expressions

I have a string "foo\nbar=blah", which I'd like to entirely replace
with "baz=blah". Supposedly, the regular expression constructed with
/.../i will match newlines, but it doesn't seem to work:

str = "foo\nbar=blah"

=> "foo\nbar=blah"

str.sub(/.*bar/i, 'baz')

=> "foo\nbaz=blah"

str.sub(/\A.*bar/i, 'baz')

=> "foo\nbar=blah"

Is there any way to do this? Does the /.../i really do what I think
it should do?

/i makes the regexp case insensitve, use /m for multiline e.g. dot
matches newline.

irb(main):001:0> "test\nit".gsub /./, 'X'
=> "XXXX\nXX"
irb(main):002:0> "test\nit".gsub /./m, 'X'
=> "XXXXXXX"

regards,

Brian

···

On 22/07/05, tsuraan <tsuraan@gmail.com> wrote:

I have a string "foo\nbar=blah", which I'd like to entirely replace
with "baz=blah". Supposedly, the regular expression constructed with
/.../i will match newlines, but it doesn't seem to work:

> str = "foo\nbar=blah"
=> "foo\nbar=blah"
> str.sub(/.*bar/i, 'baz')
=> "foo\nbaz=blah"
> str.sub(/\A.*bar/i, 'baz')
=> "foo\nbar=blah"

Is there any way to do this? Does the /.../i really do what I think
it should do?

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

I guess I meant /.../s, which one site said was " '.' matches newline
mode", but anyhow, that works great. Thanks!

···

On 22/07/05, Brian Schröder <ruby.brian@gmail.com> wrote:

On 22/07/05, tsuraan <tsuraan@gmail.com> wrote:
> I have a string "foo\nbar=blah", which I'd like to entirely replace
> with "baz=blah". Supposedly, the regular expression constructed with
> /.../i will match newlines, but it doesn't seem to work:
>
> > str = "foo\nbar=blah"
> => "foo\nbar=blah"
> > str.sub(/.*bar/i, 'baz')
> => "foo\nbaz=blah"
> > str.sub(/\A.*bar/i, 'baz')
> => "foo\nbar=blah"
>
> Is there any way to do this? Does the /.../i really do what I think
> it should do?
>
>

/i makes the regexp case insensitve, use /m for multiline e.g. dot
matches newline.

irb(main):001:0> "test\nit".gsub /./, 'X'
=> "XXXX\nXX"
irb(main):002:0> "test\nit".gsub /./m, 'X'
=> "XXXXXXX"

regards,

Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

WARNING!! Ruby and Perl are very different here; don't try to follow Perl
documentation on regular expressions.

Ruby: /foo/ = Perl: /foo/m

Ruby: /foo/m = Perl: /foo/ms

In Ruby, /foo/s means match the string with Shift-JIS (Japanese encoding),
which is quite likely not what you want :slight_smile:

(Unfortunately, I don't think ri documents regexp patterns and modifiers,
unlike perl's "man perlre". I end up referring to the Pickaxe book p324 for
this)

There is no way to turn off the equivalent of Perl's /m flag. This is
important if you are using regexps to validate data: ^ and $ do not just
match the start and end of string, they also match at newlines (\n).

    foo.untaint if foo =~ /^[a-z]*$/ # DANGEROUS

    foo.untaint if foo =~ /\A[a-z]*\z/ # correct

Regards,

Brian.

···

On Sat, Jul 23, 2005 at 03:03:24AM +0900, tsuraan wrote:

I guess I meant /.../s, which one site said was " '.' matches newline
mode", but anyhow, that works great. Thanks!