regex with pipes?
Posted: Mon Aug 05, 2013 10:32 pm
I ran across some old code using regex like this for allowing alphanumeric with dot, dash and underscores:
I was surprised to find it actually seemed to work, but I don't know why. The period isn't escaped and what's up with the pipes? I've only seen patterns done like /pattern/. I've found some cases where a similar regex from the same coder fails which makes me suspect they weren't properly tested. For example:
Seems to pass anything that has at least letter in it, but it is missing the string start ^ and $ so I would assume that it looks for one passing condition on any character and then returns a boolean result. The programmer was using this to validate alpha character strings, which obviously isn't correct, so I'm suspicious about all of the regex patterns.
Code: Select all
preg_match('|^[0-9.a-zA-Z_-]*$|', $value)Code: Select all
preg_match('|[a-zA-Z]|', $value)