Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Regular expressions

by tlm (Prior)
on Apr 13, 2005 at 09:26 UTC ( [id://447366]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Regular expressions

What you want is

if ($temp2 =~ m/^([a-zA-Z]+)$/) { $naam = $1; } else { die "Wrong input"; }
What you wrote matches only strings that contain the substring 'a-zA-Z' in them. You need to enclose ranges like 'A-Z' within [ ] for them to be interpreted as character classes. Also, by anchoring the regexp (with ^ and $1) you ensure that the entire string must match. The last thing is that you need that quantifier + so that one or more characters will match; without it, the only strings that will match are those consisting of a single letter.

1Some people prefer to use \z instead of $ for the end-of-string anchor, because $ doesn't unconditionally match the end of the string; indeed, it ignores one optional trailing newline. I think of $ as basically shorthand for /\n?\z/... Well, it's more complicated than that, because the meaning of $ changes in the presence of the /m modifier, but not so for /\n?\z/... A lot of ins, a lot of outs, a lot of what-have-yous, a lot of strands to keep in the ol' duder's head...

Update: In my original reply, I focused on the why the regexp was not matching, and failed to notice that you were using $1 later on. I fixed the regexp to reflect this. Sorry for the confusion.

the lowliest monk

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://447366]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.