Re: A couple of questions regarding runtime generation of REGEXP's
- From: Martien Verbruggen <mgjv@xxxxxxxxxxxxxxxxx>
- Date: Mon, 3 Nov 2008 13:37:21 +1100
On Mon, 03 Nov 2008 00:24:30 GMT,
sln@xxxxxxxxxxxxxxx <sln@xxxxxxxxxxxxxxx> wrote:
I'm probably going to use some wrong terms here but I
hope to give enough detail that I can get a definative
resolution to this, once and for all.
Basically I'm writing a sub that wants to take a regular
expression as a parameter. It then blindly operates on data,
matching, and posible substitution.
Apparently qr// will only function on the matching side, something like this:
# works
$rx = qr/\Q$sometext\E/s;
$data =~ /$rx/;
# or $data =~ $rx/
The matching is done by the // operator. Not because you happened to use
qr// a bit earlier.
But this:
# does not work, no way no how
$rx = qr{s/\Q$sometext\E/junk/g};
$data =~ $rx;
A bare regex is simply not going to work on the right hand side of a =~
operator. It's the operator on the right hand side that does the
matching, not the =~ operator itself. That only binds an expression
instead of $_ to that matching operator.
More detail:
From perlop:
Binary "=~" binds a scalar expression to a pattern match. Certain
operations search or modify the string $_ by default. This operator
makes that kind of operation work on some other string. The right
argument is a search pattern, substitution, or transliteration.
Note that 'pattern' or 'regular expression' are not part of the allowed
right arguments.
Further down in the same document, under "Quote and Quote-like
Operators":
Customary Generic Meaning Interpolates
'' q{} Literal no
"" qq{} Literal yes
‘‘ qx{} Command yes*
qw{} Word list no
// m{} Pattern match yes*
qr{} Pattern yes*
s{}{} Substitution yes*
tr{}{} Transliteration no (but see below)
<<EOF here-doc yes*
And a little further down again:
Regexp Quote-Like Operators
Here are the quote-like operators that apply to pattern matching and
related activities.
[snip]
Martien
--
|
Martien Verbruggen | Computers in the future may weigh no more
| than 1.5 tons. -- Popular Mechanics, 1949
|
.
- References:
- Prev by Date: Re: if ($line == $count++)
- Next by Date: Re: A couple of questions regarding runtime generation of REGEXP's
- Previous by thread: A couple of questions regarding runtime generation of REGEXP's
- Next by thread: Re: A couple of questions regarding runtime generation of REGEXP's
- Index(es):
Relevant Pages
|