Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Regex - Char Class Question

by sth (Priest)
on Dec 10, 2003 at 18:20 UTC ( [id://313801]=perlquestion: print w/replies, xml ) Need Help??

sth has asked for the wisdom of the Perl Monks concerning the following question:

I think I'm missing something here, I hope someone can't tell me why this fails,

DB<6> $a = 'Apr-Jun03.zip~'; DB<7> p $a =~ /^[\w.-~]+$/ DB<8> p $a =~ /^[~\w.-]+$/ 1 DB<9> p $a Apr-Jun03.zip~ DB<10> p $a =~ /^[\w~.-]+$/ 1 DB<11> p $a =~ /^[\w.~-]+$/ 1

...so if I move the '~' off the end of the character class, the match works. I can't figure out why. Any ideas?

Thanks, sth

Replies are listed 'Best First'.
Re: Regex - Char Class Question
by Enlil (Parson) on Dec 10, 2003 at 18:28 UTC
    I don't think it is so much the tilde(~)being at the end but the hyphen (-) not being on one of the ends. That is, if the hyphen is not at either end of the character class then it becomes a range for whatever two chars it is in between.This should work the same as all the others for instance:
    $a =~ /^[-\w.~]+$/

    -enlil

      See perlop. If you're going to put a ] or - character into your character class then it has to be either escaped or be placed at the front after the optional ^,
        See perlop.

        Actually, see perlre.

        If you're going to put a ] or - character into your character class then it has to be either escaped or be placed at the front after the optional ^,

        Or placed at the end immediately before the closing square bracket.

        The relevant passage from perlre:

        If you want either "-" or "]" itself to be a member of a class, put it at the start of the list (possibly after a "^"), or escape it with a backslash. "-" is also taken literally when it is at the end of the list, just before the closing "]".

        -sauoq
        "My two cents aren't worth a dime.";
        

        I knew I could escape the '-', I just wanted figure out what I was missing, was driving me nuts.

        Thanks for the reply!

      ..Thanks Enlil! I knew it was something obvious, just needed someone to point it out to me. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://313801]
Approved by talexb
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: (7)
As of 2024-04-19 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found