General Community > Scripting Help

vars in regex

(1/1)

Anguz:
how do I use a variable in a regular expression?  ???

[Unknown]:
Perl:

$match =~ /$blah/i;

PHP:

preg_match('/' . preg_quote($blah, '/') . '/i', $match);

-[Unknown]

Anguz:
I see... and if I have more than the variable in the regex, do I escape the metacharacters as usual even though they're single quoted?

[Unknown]:
Yes, but no need to use double slashes...

\n, \\, etc. work fine.

-[Unknown]

Anguz:
thank you  :)

Navigation

[0] Message Index

Go to full version