On Sat, Sep 18, 2010 at 8:41 PM, Brian Candler <b.candler / pobox.com> 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 =3D File.open(filename)
>> f.each do|line|
>> =A0 puts line
>> =A0 puts line.match( %r{;$})
>> =A0 puts line.match( %r{;\z})
>> =A0 puts line.match( %r{(.*);$})
>> =A0 puts line.match( %r{(.*);\Z})
>> end
>>
>> result:
>> ABC DEF=3D"";
>> nil
>> nil
>> nil
>> nil
>
> Change
> =A0puts line
> to
> =A0puts line.inspect
>
> Then you'll see that the line you've read is probably
>
> "ABC DEF=3D\"\";\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 =3D "ABC DEF=3D\"\";\n"
> =3D> "ABC DEF=3D\"\";\n"
>>> line =3D~ %r{(.*);$}
> =3D> 0

OP, I suggest you do this and post the outcome - then we can see what
goes wrong:

RXS =3D [
  %r{;$},
  %r{;\z},
  %r{(.*);$},
  %r{(.*);\Z},
]

File.foreach filename do |line|
  p line
  RXS.each {|r| printf "  rx=3D%p match=3D%p\n", r, r.match(line)}
end

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/