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.
B477627cd68ba9b9f24fc81a08c22fcf?d=identicon&s=25 Wmute (Guest)
on 2006-02-03 14: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.
B97225f66bb5caac601b12735d430a0d?d=identicon&s=25 Marcin MielżyÅ?ski (Guest)
on 2006-02-03 15: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
F91f1034d620825594db83db92ca1711?d=identicon&s=25 Michael Ulm (Guest)
on 2006-02-03 15: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 Ulm
R&D Team
ISIS Information Systems Austria
tel: +43 2236 27551-219, fax: +43 2236 21081
e-mail: michael.ulm@isis-papyrus.com
Visit our Website: www.isis-papyrus.com
B477627cd68ba9b9f24fc81a08c22fcf?d=identicon&s=25 Wmute (Guest)
on 2006-02-03 15:24
(Received via mailing list)
Thanks! problem solved!
This topic is locked and can not be replied to.