How would I make this preg_match() reg expression ignore whitespace?
I have this regular expression:
if(preg_match("/^(\\d+)(,)(\\d+)$/") ) { ... }
which tests that the user has entered a pair of numbers separated by a comma (e.g. 120,80). I've got that part working properly. However I anticipate that users may accidentally enter white space between any of the characters. I would like to make the expression ignore ALL white space characters, no matter where they occur in the pattern. I've tried this:
if(preg_match("/x^(\\d+)(,)(\\d+)$/x") ) { ... }
And also this:
if(preg_match("/(^(\\d+)(,)(\\d+)开发者_运维知识库)$/x") ) { ... }
But nothing seems to work. Any insights would be greatly appreciated. BTW I'm still learning this stuff so take that into account! Thanks. :D
You might just strip whitespace before you do your regex match.
$input = str_replace(" ", "", $input);
Try:
if(preg_match("/^\s*\d+\s*,\s*\d+\s*$/",$input) ) {
// $input has two numbers separated by a comma and may have whitespace
// around the number and/or around the comma.
}
精彩评论