From: Daniel DeLorme Date: 2008-09-29T13:49:23+09:00 Subject: Re: multiline regexp and newlines Markus Schirp wrote: > So my Regexp is like: > > "Match any character from the begin to the end of a string witch is not > newline, and ensure there is at least one character, and to this match > over multiple lines (so if there are multiple lines reject it)" > > Results in /^[^\n]+$/m > > My ruby 1.8.6 does: > > /^[^\n]+$/m =~ "a\n" -> 0 > > without multiline flag the same: > > /^[^\n]+$/ =~ "a\n" -> 0 The multiline flag only changes the meaning of "." to include the \n character. It doesn't change the meaning of ^ and $ which mean respectively "beginning of line" and "end of line". True true "beginning of string" metacharacter is not ^ but \A, and for "end of string" it's not $ but \z (or \Z if you want to allow a trailing \n on your string) >> "a\nb\n"[ /^.*$/ ] => "a" >> "a\nb\n"[ /\A.*\z/ ] => nil >> "a\nb\n"[ /\A.*\z/m ] => "a\nb\n" Yeah, it's tricky. -- Daniel