Variable matching

R

Rove Monteux

Hi,

Quite new to regex myself, Im trying to match a chunk of the text with
the following,

chunk = content.scan(/<!--Start Main Content Table -->(.*)<!--fr: End
Main Content Table -->/m)

That works alright, and its contents returned to the chunk string. Now
Im trying to do that way,

$start = "<!--Start Main Content Table -->"
$end = "<!--fr: End Main Content Table -->"

chunk = content.scan(/$start(.*)$end/m)

Obviously that doesnt works, suppose because of $ being a special
variable ? So my question is how to do pattern matching using variables
like that ?


Thanks !


Rove
 
K

Kristof Bastiaensen

Hi,

$start = "<!--Start Main Content Table -->"
$end = "<!--fr: End Main Content Table -->"

chunk = content.scan(/$start(.*)$end/m)

Obviously that doesnt works, suppose because of $ being a special
variable ?

Ehm, no, $ in a regexp means itself, or the end if used at
the end of the regexp. $ in a string means just $.
If you want variable replacement, use #{ expression }. Try
this:
chunk = content.scan(/#{start}(.*)#{end}/m)

Kristof
 

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

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top