Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Regex fun

by ikegami (Patriarch)
on Dec 15, 2009 at 20:39 UTC ( [id://812935]=note: print w/replies, xml ) Need Help??


in reply to Regex fun

I would assume that quantifier cannot be a '\x' variable, but don't really know.

It's simpler than that: The quantifier cannot be variable. I presume it could be, but no one's done the work.

Replies are listed 'Best First'.
Re^2: Regex fun
by JadeNB (Chaplain) on Dec 15, 2009 at 21:08 UTC
    It's simpler than that: The quantifier cannot be variable.
    This must mean something other than what it seems (to me) to mean:
    $ perl -E 'my $quant = 2; "ab" =~ /.{$quant}/ and say "Matched"' Matched
    shows that the ‘length’ of the quantifier can be given by a variable.

      I didn't say you couldn't build regexps dynamically. I said the quantifier can't be variable. Perl regular expressions don't even have variables, so you couldn't possibly have shown one being used.

      You can't have a quantifier until you have a regexp, and you don't have a regexp until you interpolate anything that needs to be interpolated.

      What's the quantifier in the following?

      $ perl -E 'my $x = "2"; "ab" =~ /.{$x}/ and say "Matched"' Matched

      Are you saying it's different in this one?

      $ perl -E 'my $x = "{2}"; "ab" =~ /.$x/ and say "Matched"' Matched

      What about this one?

      $ perl -E 'my $x = "2}"; "ab" =~ /.{$x/ and say "Matched"' Matched

      In all cases, the quantifier is {2}. No variables is involved. Sure, the regexp is produced from a variable, but that has nothing to do with the quantifier.

      Update: Clarified.

        Perl regular expressions don't even have variables, so you couldn't possibly have shown one being used.

        This is what I first thought you meant, but it didn't make any sense to me. If one distinguishes “regex variables” (variable at regex run-time) from “Perl variables” (constant at each regex run-time *), then, as you (basically) say, the only ones (I think) are the \1 back-references; but, when Hena said

        I would assume that quantifier cannot be a '\x' variable
        you said
        It's simpler than that: The quantifier cannot be variable.
        The only reason I could think of to make this comment is if there were some other sort of variable, but you seem to be saying that there isn't. Did I misunderstand?

        UPDATE: * False, as I finally realised in Re^6: Regex fun.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://812935]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-06-22 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.