用于数字比较的正则表达式?
如果从输入匹配数据库中的数据输入 5 位数字,我想执行正则表达式以返回 true/false,不需要满足序列,但需要确切的数字。
例如:在数据库中,我有 12345
当我在搜索中输入 5 位数字时,我想知道它是否与 12345 中的每个数字匹配。
如果我输入 34152- 它应该返回 true
如果我输入 14325-它应该返回 true
如果我输入 65432 - 它应该返回 false
如果我输入 11234 - 它应该返回 false
例如:在数据库中我有 44512
如果我输入 21454 - 它应该返回 true
如果我输入 21455 - 它应该返回false
如何使用 php 和正则表达式来执行此操作
I would like to perform regex to return true/false if the input 5 digit from input matching data in database, no need to cater of the sequence, but need the exact numbers.
Eg: In database I have 12345
When I key in a 5 digit value into search, I want to find out whether it is matching the each number inside the 12345.
If I key in 34152- it should return true
If I key in 14325- it should return true
If I key in 65432- it should return false
If I key in 11234- it should return false
Eg: In database I have 44512
If I key in 21454- it should return true
If I key in 21455- it should return false
How to do this using php with regex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是避免正则表达式的一种方法
This is a way avoiding regex
好吧,我可以告诉你,这不会是一个简单的正则表达式。您可以执行如下操作:
现在,由于 MySQL 不支持正则表达式中的断言,因此您需要在多个正则表达式中执行此操作:
这将产生:
这应该可以为您完成。
它并不漂亮,但它应该为您处理所有可能的排列,假设您存储的数据格式是一致的...
但是,根据您的需要,您应该尝试将其取出并在 PHP 中处理它。在这种情况下,正则表达式会简单得多:
或者,您也可以在插入数字之前对其进行预排序。因此,您无需插入
14231
,而是插入11234
。这样,您始终知道序列已正确排序,因此您只需执行numberField = '11234'
而不是上面那个巨大的野兽......Well, it's not going to be a trivial regex, I can tell you that. You could do something like this:
Now, since MySQL doesn't support assertions in regex, you'll need to do this in multiple regexes:
That'll produce:
That should do it for you.
It's not pretty, but it should take care of all of the possible permutations for you, assuming that your stored data format is consistent...
But, depending on your needs, you should try to pull it out and process it in PHP. In which case the regex would be far simpler:
Or, you could also pre-sort the number before you insert it. So instead of inserting
14231
, you'd insert11234
. That way, you always know the sequence is ordered properly, so you just need to donumberField = '11234'
instead of that gigantic beast above...尝试使用
当您有重复的数字时,这会变得更加复杂。
你真的不应该用正则表达式这样做。 =)
Try using
This will get much more complicated, when you have duplicate numbers.
You really should not do this with regex. =)