Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^4: Regex fun

by JadeNB (Chaplain)
on Dec 15, 2009 at 21:59 UTC ( [id://812943]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Regex fun
in thread Regex fun

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.

Replies are listed 'Best First'.
Re^5: Regex fun
by ikegami (Patriarch) on Dec 15, 2009 at 22:22 UTC

    The only reason I could think of to make this comment is if there were some other sort of variable

    Or if someone posts an SoPW asking how to make it variable.

    Actually, it was to say that there wasn't "some other sort of variable". The OP's supposition implied that variables could be used there. They can't. That's what I said.

    Aside from that, did you forget about capture buffers, the return value of the last (?{ }) and Perl variables? There's just no way to access them there.

    Update: Added second and third para.

      Thanks for the additional paragraphs.

      The OP's supposition implied that variables could be used there. They can't. That's what I said.

      That's not how I read

      I would assume that quantifier cannot be a '\x' variable
      at all—it seems to me specifically to be saying that you can't use a variable there. Do you mean that, since it mentions that you can't use one sort of variable, it implies that you can use others?

      Aside from that, did you forget about capture buffers, the return value of the last (?{ }) and Perl variables? There's just no way to access them there.

      You certainly can use, at least in the sense of referring to, capture buffers (which I guess means $1 instead of \1?) and Perl variables, like /(.{$1}){$a}/; since their values don't change during the regex match *, the result is the same (as you pointed out) as substituting in their values before running the regex. (Indeed, I think that's exactly what happens internally.) This is just what I meant in Re: Regex fun; so, as I indicated there, I must (still) be misunderstanding you.

      UPDATE: To avoid the appearance of slyly avoiding the bit about the return value of the last (?{ }), I didn't forget about it, but only because I never knew about it. Thanks!

      Another UPDATE: * Ah, this is probably a crucial point! I always assumed that $1 didn't change until the match had completed; but I guess it's more accurate to say that it does change, but that all the interpolation has already happened at that point, so that change is not reflected in the regex. There's my misunderstanding; sorry!

        it seems to me specifically to be saying that you can't use a variable there.

        Then why did the OP say "Can't use a \x variable" instead of "can't use a variable"?

        If I say "The Western sky is orange", doesn't that tell you something about the Eastern sky? At the very least, I'm leaving the possibility of the Eastern sky being a different colour. In fact, it's quite likely it's either a different colour or I don't know it's colour.

        capture buffers (which I guess means $1 instead of \1?)

        I mean "what is captured". Technically, they exist as a reference to the string being matched, a start position and an end position (or something similar).

        $1 and \1 are both windows into them, and so are @-, @+, %-,%+ and $^N.

        since their values don't change during the regex match

        They're not used at all during regex matching. There isn't even a regex opcode to refer to them. The only way they can be accessed is via \1 (etc) and via Perl code blocks ((?{ }) and (??{ }). Neither of those can be used in a quantifier.

        And if they don't change — if you can't even give them a value — how can you consider them variables?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-29 12:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found