# Ruby regexpresion, error? :-(

**URL:** <https://www.ruby-forum.com/t/ruby-regexpresion-error/195184>\
**Category:** Ruby\
**Created:** [September 18, 2010, 4:23pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion-error/195184 "2010-09-18T16:23:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![imelody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e274bd/32.png) [@imelody](https://www.ruby-forum.com/u/imelody)\
**Post date:** [September 18, 2010, 4:23pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion-error/195184/1 "2010-09-18T16:23:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![imelody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e274bd/32.png) [@imelody](https://www.ruby-forum.com/u/imelody)\
**Post date:** [September 18, 2010, 4:43pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion-error/195184/2 "2010-09-18T16:43:11Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![imelody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e274bd/32.png) [@imelody](https://www.ruby-forum.com/u/imelody)\
**Post date:** [September 18, 2010, 5:01pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion-error/195184/3 "2010-09-18T17:01:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![imelody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e274bd/32.png) [@imelody](https://www.ruby-forum.com/u/imelody)\
**Post date:** [September 18, 2010, 5:02pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion-error/195184/4 "2010-09-18T17:02:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![imelody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e274bd/32.png) [@imelody](https://www.ruby-forum.com/u/imelody)\
**Post date:** [September 18, 2010, 5:01pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion-error/195184/5 "2010-09-18T17:01:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![imelody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e274bd/32.png) [@imelody](https://www.ruby-forum.com/u/imelody)\
**Post date:** [September 18, 2010, 5:11pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion-error/195184/6 "2010-09-18T17:11:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![imelody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e274bd/32.png) [@imelody](https://www.ruby-forum.com/u/imelody)\
**Post date:** [September 18, 2010, 8:40pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion-error/195184/7 "2010-09-18T20:40:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![imelody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e274bd/32.png) [@imelody](https://www.ruby-forum.com/u/imelody)\
**Post date:** [September 20, 2010, 9:26am UTC](https://www.ruby-forum.com/t/ruby-regexpresion-error/195184/8 "2010-09-20T09:26:40Z")

</div>

On Sat, Sep 18, 2010 at 8:41 PM, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![imelody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e274bd/32.png) [@imelody](https://www.ruby-forum.com/u/imelody)\
**Post date:** [September 18, 2010, 5:23pm UTC](https://www.ruby-forum.com/t/ruby-regexpresion-error/195184/9 "2010-09-18T17:23:40Z")

</div>

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
