From: Robert Klemme Date: 2010-09-20T16:25:56+09:00 Subject: Re: Ruby regexpresion, error? :-( On Sat, Sep 18, 2010 at 8:41 PM, Brian Candler wrote: > iMelody Ooo wrote: >> Dear Brian Candler, thank you very much, but still have problem. >> >> I read a line from file,want to macth ";" >> >> f = File.open(filename) >> f.each do|line| >>   puts line >>   puts line.match( %r{;$}) >>   puts line.match( %r{;\z}) >>   puts line.match( %r{(.*);$}) >>   puts line.match( %r{(.*);\Z}) >> end >> >> result: >> ABC DEF=""; >> nil >> nil >> nil >> nil > > Change >  puts line > to >  puts line.inspect > > Then you'll see that the line you've read is probably > > "ABC DEF=\"\";\n" > > (i.e. there's a newline at the end. \" is how inspect shows a > double-quote within a double-quoted string) > > I would have expected %r{(.*);$} to match though: > >>> line = "ABC DEF=\"\";\n" > => "ABC DEF=\"\";\n" >>> line =~ %r{(.*);$} > => 0 OP, I suggest you do this and post the outcome - then we can see what goes wrong: RXS = [ %r{;$}, %r{;\z}, %r{(.*);$}, %r{(.*);\Z}, ] File.foreach filename do |line| p line RXS.each {|r| printf " rx=%p match=%p\n", r, r.match(line)} end Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/