# Problem with regexp?

**URL:** <https://rubytalk.org/t/problem-with-regexp/24593>\
**Category:** ruby-talk\
**Created:** [3 February 2006 13:38 UTC](https://rubytalk.org/t/problem-with-regexp/24593 "2006-02-03T13:38:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wmute](https://avatars.discourse-cdn.com/v4/letter/w/278dde/32.png) [@Wmute](https://rubytalk.org/u/Wmute)\
**Post date:** [3 February 2006 13:38 UTC](https://rubytalk.org/t/problem-with-regexp/24593/1 "2006-02-03T13:38:18Z")

</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:** ![Marcin\_Mielzynski](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Marcin\_Mielzynski](https://rubytalk.org/u/Marcin_Mielzynski)\
**Post date:** [3 February 2006 14:08 UTC](https://rubytalk.org/t/problem-with-regexp/24593/2 "2006-02-03T14:08:20Z")

</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:** ![Michael\_Ulm](https://avatars.discourse-cdn.com/v4/letter/m/a9adbd/32.png) [@Michael\_Ulm](https://rubytalk.org/u/Michael_Ulm)\
**Post date:** [3 February 2006 14:16 UTC](https://rubytalk.org/t/problem-with-regexp/24593/3 "2006-02-03T14:16:04Z")

</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 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](http://www.isis-papyrus.com)
> 
> ---------------------------------------------------------------  
> This e-mail is only intended for the recipient and not legally  
> binding. Unauthorised use, publication, reproduction or  
> disclosure of the content of this e-mail is not permitted.  
> This email has been checked for known viruses, but ISIS accepts  
> no responsibility for malicious or inappropriate content.  
> ---------------------------------------------------------------

---

<div class="post-metadata">

**Author:** ![Wmute](https://avatars.discourse-cdn.com/v4/letter/w/278dde/32.png) [@Wmute](https://rubytalk.org/u/Wmute)\
**Post date:** [3 February 2006 14:23 UTC](https://rubytalk.org/t/problem-with-regexp/24593/4 "2006-02-03T14:23:18Z")

</div>

Thanks! problem solved!
