Forum: Ruby problem with regexp?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Wmute (Guest)
on 2006-02-03 15:38
(Received via mailing list)
Hello,

I am trying to replace some old Perl scripts with Ruby, however

i get stuck with this line

>>

if line =~/($time)/ && line =~/(xxx)/

<<
$time is a global variable containing the current day/month, but it
wont match!
what am i doing wrong?


I am new to Ruby, so please dont be offended.
Marcin MielżyÅ?ski (Guest)
on 2006-02-03 16:11
(Received via mailing list)
Wmute wrote:

>
> if line =~/($time)/ && line =~/(xxx)/
>

>
in Ruby $..  wont reference a variable like in perl, use #{...}


if line =~ /#{time}/ && line =~ /(xxx)/ ....

in case of /#{(time)}/ regexp will be compiled each time...

so you may consider:

r = /#{time}/ , or:
r = Regexp.new(time)


....
probably some loop
....

if line =~ r && ...

lopex
Michael U. (Guest)
on 2006-02-03 16:18
(Received via mailing list)
Wmute wrote:

> Hello,
>
> I am trying to replace some old Perl scripts with Ruby, however
>
> i get stuck with this line
>
>
>
> if line =~/($time)/ && line =~/(xxx)/

You probably want

if line =~ /#{$time}/ and line =~ /#{xxx}/

(assuming there is a variable called xxx).

HTH

Michael

--
Michael U.
R&D Team
ISIS Information Systems Austria
tel: +43 2236 27551-219, fax: +43 2236 21081
e-mail: removed_email_address@domain.invalid
Visit our Website: www.isis-papyrus.com
Wmute (Guest)
on 2006-02-03 16:24
(Received via mailing list)
Thanks! problem solved!
This topic is locked and can not be replied to.