Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pattern matching help 1

Status
Not open for further replies.

Graziella

Programmer
Jul 8, 2004
38
AT
Hi,

I have a list of patterns, for instance,
$pattern = "l\,?454\s*\-?\s*f(oo|ee)t";

The usual way of looking for that pattern in a string, that is,
if ($test =~ /$pattern/){ #do something }
does not work.
How could I tell perl that $pattern is an already formed regular expression ?

Thank you for any help.

Grazia
 
Your original test works fine (I tested it). Try it with an easier regex and work your way up until you find whats causing it to not match.

The problem lies in your regex, not the interpolation of the variable.
 
Check out qr//.
$pattern = qr/l,?454\s*-?\s*f(oo|ee)t/;

I removed the \ before the comma and before the -. Commma is never special and - is special only in char classes, when not the first or last character.

As an alternative, you could just put the expression in single quotes (''). If you use double quotes, ("") you need 2 backslashes to equal a single backslash.

HTH

 
Hi Mikevh,

may I ask you why "qr".
Maybe you meant "qw"

Grazia
 
qr// complies a string into a regexp. qw// produces a list of words from a space-seperated collection of words.

You can read more on them in perlop, under `regexp quote-like operators'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top