Ruby regexpresion, error? :-(

I want to match last chars of the line,

def show_regexp(a,re)
if a =~ re
“#{$`}<<#{$&}>>#{$’}”
else
“no match”
end
end

puts show_regexp(“abc as;”,/^a/)
puts show_regexp(“abc as;”,/as$/)
puts show_regexp(“abc as;”,/\zas/)
puts show_regexp(“abc as;”,/\Zas/)
puts show_regexp(“abc as;”,/(.)as$/)
puts show_regexp(“abc as;”,/(.
)\zas/)
puts show_regexp(“abc as;”,/(.*)\Zas/)

Result:
<>bc as;
no match
no match
no match
no match
no match
no match

iMelody O. wrote:

puts show_regexp(“abc as;”,/as$/)

/as$/ means “match ‘a’ then ‘s’ then end-of-line”. But there is a
semicolon after the ‘s’ in the source string, so the match fails.

puts show_regexp(“abc as;”,/\zas/)

\z matches end of string, and you’ve asked to match characters beyond
the end of string!

Your other cases are just variants of this.

Try matching with /as;$/ or /as;\z/

[There are subtle differences. \z matches only the end of the string. \Z
matches end of string or a newline character at the end of the string. $
matches end of string or a newline anywhere within the string]

Also, don’t bother with a show_regexp function while you’re working this
out. Just use irb to try it out interactively.

$ irb --simple-prompt

src = “abc as;”
=> “abc as;”

src =~ /as$/
=> nil

src =~ /as;$/
=> 4

[$`, $&, $’]
=> ["abc ", “as;”, “”]

nil means no match, 4 means matched at position 4 (counting 0 as the
first character of the string)

Dear Brian C., 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

iMelody O. wrote:

Dear Brian C., 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

then do this
f = File.open(filename)
f.each do|line|
puts line
puts “found ;” if line[-1]==";"
end

iMelody O. wrote:

I want to match last chars of the line,

def show_regexp(a,re)
if a =~ re
“#{$`}<<#{$&}>>#{$’}”
else
“no match”
end
end

puts show_regexp(“abc as;”,/^a/)
puts show_regexp(“abc as;”,/as$/)
puts show_regexp(“abc as;”,/\zas/)
puts show_regexp(“abc as;”,/\Zas/)
puts show_regexp(“abc as;”,/(.)as$/)
puts show_regexp(“abc as;”,/(.
)\zas/)
puts show_regexp(“abc as;”,/(.*)\Zas/)

Result:
<>bc as;
no match
no match
no match
no match
no match
no match

i see that you are actually wanting to match “as” at the end of
line/string. There is no need to use regexp.

irb(main):001:0> s=“abc as;”
=> “abc as;”
irb(main):002:0> s[-2,2]==“as”
=> false

Thank you,Steel S.,But why cannot use regexpresion?

then do this
f = File.open(filename)
f.each do|line|
puts line
puts “found ;” if line[-1]==";"
end

iMelody O. wrote:

Dear Brian C., 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

On Sat, Sep 18, 2010 at 8:41 PM, Brian C. [email protected]
wrote:

puts line.match( %r{(.*);$})
Change

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

iMelody O. wrote:

Thank you,Steel S.,But why cannot use regexpresion?

then do this
f = File.open(filename)
f.each do|line|
puts line
puts “found ;” if line[-1]==";"
end

yes you can use regexp. But your requirement is simple. So simple string
indexing does the job. by the way, the reason why it did not work for
you is that you have a newline character when a line is read. So just
chomp it off

f = File.open(filename)
f.each do|line|
line.chomp!
puts “found ;” if line[-1]==";"
end

same to your regexp method

f = File.open(filename)
f.each do|line|
puts line
line.chomp!
puts line.match( %r{;$})
puts line.match( %r{;\z})
puts line.match( %r{(.);$})
puts line.match( %r{(.
);\Z})
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs