# Problem with regexp?

**URL:** <https://www.ruby-forum.com/t/problem-with-regexp/100644>\
**Category:** Ruby\
**Created:** [February 3, 2006, 2:38pm UTC](https://www.ruby-forum.com/t/problem-with-regexp/100644 "2006-02-03T14:38:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wmute](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/278dde/32.png) [@Wmute](https://www.ruby-forum.com/u/Wmute)\
**Post date:** [February 3, 2006, 2:38pm UTC](https://www.ruby-forum.com/t/problem-with-regexp/100644/1 "2006-02-03T14:38:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Wmute](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/278dde/32.png) [@Wmute](https://www.ruby-forum.com/u/Wmute)\
**Post date:** [February 3, 2006, 3:11pm UTC](https://www.ruby-forum.com/t/problem-with-regexp/100644/2 "2006-02-03T15:11:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wmute](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/278dde/32.png) [@Wmute](https://www.ruby-forum.com/u/Wmute)\
**Post date:** [February 3, 2006, 3:18pm UTC](https://www.ruby-forum.com/t/problem-with-regexp/100644/3 "2006-02-03T15:18:00Z")

</div>

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](http://www.isis-papyrus.com)

---

<div class="post-metadata">

**Author:** ![Wmute](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/278dde/32.png) [@Wmute](https://www.ruby-forum.com/u/Wmute)\
**Post date:** [February 3, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/problem-with-regexp/100644/4 "2006-02-03T15:24:06Z")

</div>

Thanks! problem solved!
