[tpm] Dumb regex question

Andy Jack andy+lists at veracity.ca
Tue Aug 21 09:56:04 PDT 2007


On Tue, Aug 21, 2007 at 12:15:45PM -0400, Madison Kelly wrote:
> All I want is to be able to match a word-character string that may have 
> a hyphen in it.
> 
> So:
> 
> mizu-bu		# should match
> alteeve		# should match
> m!zu-bu		# should not match
> a|teeve		# should not match

/me warms up clue-stick

if ( $str =~ m#\A[\w\-]+\z# ) {
    # matched
}
else {
    # didn't match
}

\A matches the absolute beginning of the string, \z matches the absolute
end.  \w is the alphanumeric character class [0-9A-Za-z_] depending on
your locale, and the hyphen is added since you wanted it.  + says "one
or more characters in this class".

So the regex says the string must be composed entirely of one or more
alphanumeric characters plus hyphen.

HTH Andy


More information about the toronto-pm mailing list