[Melbourne-pm] basic regex - char array vs "pipe brackets"
Christopher Short
crshort at gmail.com
Wed Mar 4 20:10:47 PST 2009
Hmm ... I've been proven wrong ... (again!)
I saw this regex in someone else's code (to check validity of an id field)
/^(\w|\@|\.|\-)+$/
and decided that it wouldn't work properly, that what they'd meant was
the character array
/^[\w\@\.\-]+$/
Luckily I chucked it into Regex Coach and found both of them worked
just as well.
Thing is, when I look at
/^(\w|\@|\.|\-)+$/
I automatically assume it will look for repeated occurences of the
first character, as this would:
/^(\w|\@|\.|\-)\1*$/
How should I remember that it doesn't work that way? That the "memory
brackets" only get used where \1 , $1 etc are specifically mentioned?
Turns out the long-term perl developer who wrote that code always uses
"pipe brackets" instead of character arrays.
Do they really function identically?
Christopher
More information about the Melbourne-pm
mailing list