RegEx bug in 1.8.2?

B

Bill Atkins

bill@kaitain ruby $ ruby --version
ruby 1.8.2 (2004-11-06) [x86_64-linux]

bill@kaitain ruby $ cat regex_x_bug.rb
regex = /(.*) # X / Y
[abcde]/x

Running this code snippet yields:

regex_x_bug.rb:1: syntax error

If the / in the comment is changed to a -, the code compiles without
complaint It looks like Ruby is interpreting the / as the end of the
regex.

Bill
 
J

Jason Sweat

bill@kaitain ruby $ ruby --version
ruby 1.8.2 (2004-11-06) [x86_64-linux]

bill@kaitain ruby $ cat regex_x_bug.rb
regex = /(.*) # X / Y
[abcde]/x

I believe you need to excape the / with a \
regex = /(.*) # X \/ Y [abcde]/x

Jason
 
J

James Edward Gray II

I believe you need to excape the / with a \
regex = /(.*) # X \/ Y [abcde]/x

You shouldn't have to with the /x modifier. It allows comments in the
regex, and that / comes after a #.

James Edward Gray II
 
L

Logan Capaldo

he shouldn't have to since he's using the /x modifier. Unfortunately I
think the problem is that Ruby has no way to know that /x is there
until it gets to it, and since it doesn't see it it assumes that / is
the end of the regex. Doing it any other way would require arbitrary
lookahead (I think, any parser guru's feel free to correct me).
Perhaps you can do something like Regexp.new "your regex in a string".
No I just tried that and it doesn't do it the right way. Aha! here we
go:
Regexp.new("(.*) # X / Y
[abcde]", Regexp::EXTENDED)

Yeah that works.

In the future I guess you'll have to avoid / in comments for extended
regular expressions, either that or escape them or use the method I
just outlined.

bill@kaitain ruby $ ruby --version
ruby 1.8.2 (2004-11-06) [x86_64-linux]

bill@kaitain ruby $ cat regex_x_bug.rb
regex = /(.*) # X / Y
[abcde]/x

I believe you need to excape the / with a \
regex = /(.*) # X \/ Y [abcde]/x

Jason
 
J

Jason Sweat

I believe you need to excape the / with a \
regex = /(.*) # X \/ Y [abcde]/x

You shouldn't have to with the /x modifier. It allows comments in the
regex, and that / comes after a #.

James Edward Gray II

Oh right, skipped right over that sublty :(
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: RegEx bug in 1.8.2?"

|> I believe you need to excape the / with a \
|> regex = /(.*) # X \/ Y [abcde]/x
|
|You shouldn't have to with the /x modifier. It allows comments in the
|regex, and that / comes after a #.

Current Ruby parser does not look into regex expression inside //, so
that Jason was right, for now at least.

matz.
 

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,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top