From: Vince H&K Date: 2007-03-24T05:48:12+09:00 Subject: Re: Knocking Lines Out Of A Multiline String Brian Candler wrote: > On Sat, Mar 24, 2007 at 03:39:42AM +0900, Rick DeNatale wrote: >> On 3/22/07, Robert Klemme wrote: >> >>> Bullshit: just use select: >>> >>>>> "foo\nbar\n".select {|l| /^f/ =~ l} >>> => ["foo\n"] >> Or, since he's really trying to exclude lines which include /usr/local/lib: >> >> str.reject {|s| Regexp.new("/usr/local/lib").match(s)} > > Why does nobody seem to anchor regexps? I have seen so many documentation > examples now which suggest that something like > > raise "Invalid data" unless /[A-Za-z0-9]+/ =~ data > > is a good example of data validation :-( I agree that it isn't... However, most of my uses of regexps are to extract some pieces of data from a bigger text - such as `identify biniou.jpg` =~ /(\d+)x(\d+)/ It would be a pain to have to unanchor those... Moreover, it is way better (in my opinion), to write something like raise "Invalid data" if /[^A-Za-z0-9]/ =~ data if you really require the full data to be alnum. Or, rather, if you want to be somehow more flexible (stripping whitespace and other): raise "Invalid data" unless /([A-Za-z0-9]+)/ =~ data data = $1 # Cleaning up data... Cheers, Vincent -- Vincent Fourmond, PhD student (not for long anymore) http://vincent.fourmond.neuf.fr/