Page 1 of 1
question about simple regex
Posted: Tue Mar 31, 2009 1:35 pm
by oboedrew
Which of these would be the preferable way to check that a variable contains at least one character that is not a hyphen?
or
According to
http://www.regular-expressions.info/reference.html, a hyphen should be escaped by a backslash unless the hyphen occurs immediately after a caret. But when I try the two versions in a php script, they both seem to work. Are they interchangeable, or am I missing something?
Thanks,
Drew
Re: question about simple regex
Posted: Thu Apr 02, 2009 3:45 am
by prometheuzz
oboedrew wrote:Which of these would be the preferable way to check that a variable contains at least one character that is not a hyphen?
or
According to
http://www.regular-expressions.info/reference.html, a hyphen should be escaped by a backslash unless the hyphen occurs immediately after a caret. But when I try the two versions in a php script, they both seem to work. Are they interchangeable, or am I missing something?
Thanks,
Drew
Hi Drew,
Yes, they're the same.
For example, these all match a single '*':
Code: Select all
'/[*]/'
// equals
'/[\*]/'
// equals
'/\*/'
What is the "best" is debatable. I prefer to use the least amount of characters in a regex, so I'd opt for option three, or if the character class is larger, go for option 1. I wouldn't use option 2 (but again: that's just me).