用于数字比较的正则表达式?

发布于 2024-10-18 05:35:28 字数 398 浏览 4 评论 0原文

如果从输入匹配数据库中的数据输入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨与橙与城 2024-10-25 05:35:28

这是避免正则表达式的一种方法

<?php
function cmpkey($a,$b){
  $aa =  str_split($a); sort($aa);
  $bb =  str_split($b); sort($bb);
  return ( implode("",$aa) == implode("",$bb));
}
?>

This is a way avoiding regex

<?php
function cmpkey($a,$b){
  $aa =  str_split($a); sort($aa);
  $bb =  str_split($b); sort($bb);
  return ( implode("",$aa) == implode("",$bb));
}
?>
琴流音 2024-10-25 05:35:28

好吧,我可以告诉你,这不会是一个简单的正则表达式。您可以执行如下操作:

$chars = count_chars($input, 1);
$numbers = array();
foreach ($chars as $char => $frequency) {
    if (is_numeric(chr($char))) {
        $numbers[chr($char)] = $frequency;
    }
}
// that converts "11234" into array(1 => 2, 2 => 1, 3 => 1, 4 => 1)

现在,由于 MySQL 不支持正则表达式中的断言,因此您需要在多个正则表达式中执行此操作:

$where = array();
foreach ($numbers AS $num => $count) {
    $not = "[^$num]";
    $regex = "^";
    for ($i = 0; $i < $count; $i++) {
        $regex .= "$not*$num";
    }
    $regex .= "$not*";
    $where[] = "numberField REGEXP '$regex'";
}

$where = '((' . implode(') AND (', $where).'))';

这将产生:

( 
        (numberField REGEXP '^[^1]*1[^1]*1[^1]*

这应该可以为您完成。

它并不漂亮,但它应该为您处理所有可能的排列,假设您存储的数据格式是一致的...

但是,根据您的需要,您应该尝试将其取出并在 PHP 中处理它。在这种情况下,正则表达式会简单得多:

^(?=.*1.*1})(?=.*2)(?=.*3)(?=.*4)\d{5}$

或者,您也可以在插入数字之前对其进行预排序。因此,您无需插入 14231,而是插入 11234。这样,您始终知道序列已正确排序,因此您只需执行 numberField = '11234' 而不是上面那个巨大的野兽......

) AND (numberField REGEXP '^[^2]*2[^2]*

这应该可以为您完成。

它并不漂亮,但它应该为您处理所有可能的排列,假设您存储的数据格式是一致的...

但是,根据您的需要,您应该尝试将其取出并在 PHP 中处理它。在这种情况下,正则表达式会简单得多:


或者,您也可以在插入数字之前对其进行预排序。因此,您无需插入 14231,而是插入 11234。这样,您始终知道序列已正确排序,因此您只需执行 numberField = '11234' 而不是上面那个巨大的野兽......

) AND (numberField REGEXP '^[^3]*3[^3]*

这应该可以为您完成。

它并不漂亮,但它应该为您处理所有可能的排列,假设您存储的数据格式是一致的...

但是,根据您的需要,您应该尝试将其取出并在 PHP 中处理它。在这种情况下,正则表达式会简单得多:


或者,您也可以在插入数字之前对其进行预排序。因此,您无需插入 14231,而是插入 11234。这样,您始终知道序列已正确排序,因此您只需执行 numberField = '11234' 而不是上面那个巨大的野兽......

) AND (numberField REGEXP '^[^4]*4[^4]*

这应该可以为您完成。

它并不漂亮,但它应该为您处理所有可能的排列,假设您存储的数据格式是一致的...

但是,根据您的需要,您应该尝试将其取出并在 PHP 中处理它。在这种情况下,正则表达式会简单得多:


或者,您也可以在插入数字之前对其进行预排序。因此,您无需插入 14231,而是插入 11234。这样,您始终知道序列已正确排序,因此您只需执行 numberField = '11234' 而不是上面那个巨大的野兽......

) )

这应该可以为您完成。

它并不漂亮,但它应该为您处理所有可能的排列,假设您存储的数据格式是一致的...

但是,根据您的需要,您应该尝试将其取出并在 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:

$chars = count_chars($input, 1);
$numbers = array();
foreach ($chars as $char => $frequency) {
    if (is_numeric(chr($char))) {
        $numbers[chr($char)] = $frequency;
    }
}
// that converts "11234" into array(1 => 2, 2 => 1, 3 => 1, 4 => 1)

Now, since MySQL doesn't support assertions in regex, you'll need to do this in multiple regexes:

$where = array();
foreach ($numbers AS $num => $count) {
    $not = "[^$num]";
    $regex = "^";
    for ($i = 0; $i < $count; $i++) {
        $regex .= "$not*$num";
    }
    $regex .= "$not*";
    $where[] = "numberField REGEXP '$regex'";
}

$where = '((' . implode(') AND (', $where).'))';

That'll produce:

( 
        (numberField REGEXP '^[^1]*1[^1]*1[^1]*

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:

^(?=.*1.*1})(?=.*2)(?=.*3)(?=.*4)\d{5}$

Or, you could also pre-sort the number before you insert it. So instead of inserting 14231, you'd insert 11234. That way, you always know the sequence is ordered properly, so you just need to do numberField = '11234' instead of that gigantic beast above...

) AND (numberField REGEXP '^[^2]*2[^2]*

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 insert 11234. That way, you always know the sequence is ordered properly, so you just need to do numberField = '11234' instead of that gigantic beast above...

) AND (numberField REGEXP '^[^3]*3[^3]*

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 insert 11234. That way, you always know the sequence is ordered properly, so you just need to do numberField = '11234' instead of that gigantic beast above...

) AND (numberField REGEXP '^[^4]*4[^4]*

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 insert 11234. That way, you always know the sequence is ordered properly, so you just need to do numberField = '11234' instead of that gigantic beast above...

) )

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 insert 11234. That way, you always know the sequence is ordered properly, so you just need to do numberField = '11234' instead of that gigantic beast above...

何处潇湘 2024-10-25 05:35:28

尝试使用

^(?=.*1)(?=.*2)(?=.*3)(?=.*4)(?=.*5).{5}$

当您有重复的数字时,这会变得更加复杂。
你真的不应该用正则表达式这样做。 =)

Try using

^(?=.*1)(?=.*2)(?=.*3)(?=.*4)(?=.*5).{5}$

This will get much more complicated, when you have duplicate numbers.
You really should not do this with regex. =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文