我如何使这个 preg_match() reg 表达式忽略空格?
我有这个正则表达式:
if(preg_match("/^(\\d+)(,)(\\d+)$/") ) { ... }
它测试用户是否输入了一对用逗号分隔的数字(例如 120,80)。我已经让那部分工作正常了。不过,我预计用户可能会意外地在任何字符之间输入空格。我想让表达式忽略所有空白字符,无论它们出现在模式中的位置。我已经尝试过这个:
if(preg_match("/x^(\\d+)(,)(\\d+)$/x") ) { ... }
还有这个:
if(preg_match("/(^(\\d+)(,)(\\d+))$/x") ) { ... }
但似乎没有任何效果。任何见解将不胜感激。顺便说一句,我仍在学习这些东西,所以请考虑到这一点!谢谢。 :D
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在进行正则表达式匹配之前删除空格。
You might just strip whitespace before you do your regex match.
尝试:
Try: