Re: A couple of questions regarding runtime generation of REGEXP's



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
|
.



Relevant Pages

  • Re: Search Ambiguous
    ... Suppose the pattern has multiple instances of '*', ... The starting point for matching frag2 depends on the accuracy of the match ... Function AmbiguousFindAndReturn(TextString As String, _ ...
    (microsoft.public.vb.general.discussion)
  • Re: [RCR] array or with non-array
    ... This requires the arg to be a String. ... I'm not sure how much more polymorphic one could get, unless one had every object present its .to_s representation for matching, which would not be good. ... The object has to match the pattern, and the patterns are descriptions of strings. ... # pos can go beyond the eof ...
    (comp.lang.ruby)
  • Re: array or with non-array
    ... David A. Black wrote: ... > matching an IO object to a pattern. ... > there *should* be an explicit, intervening string representation. ... This works pretty well for every pattern without anchors. ...
    (comp.lang.ruby)
  • Re: "string match" and "glob" pattern rules
    ... So having none glob-style pattern matching engineen would only be ... something for tcl 9. ... I've often thought that a centralised string matching engine would make ...
    (comp.lang.tcl)
  • Re: Regular Expression AND mach
    ... There are, of course, many exceptions, but the pattern is ... > matching techniques and is used by the Glimpse and Webglimpse search ... > There is a Python port of agrep available as a module called 'agrepy' ...
    (comp.lang.python)