Ruby regexpresion, error? :-(

I

iMelody Ooo

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:
<<a>>bc as;
no match
no match
no match
no match
no match
no match
 
B

Brian Candler

iMelody said:
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)
 
I

iMelody Ooo

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
 
S

Steel Steel

iMelody said:
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:
<<a>>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
 
S

Steel Steel

iMelody said:
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

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

iMelody Ooo

Thank you,Steel Steel,But why cannot use regexpresion?
then do this
f = File.open(filename)
f.each do|line|
puts line
puts "found ;" if line[-1]==";"
end
 
S

Steel Steel

iMelody said:
Thank you,Steel Steel,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
 
B

Brian Candler

iMelody said:
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:
=> 0
 
R

Robert Klemme

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:

=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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top