PHP PCRE 的问题

发布于 2024-08-07 21:26:11 字数 556 浏览 8 评论 0原文

我在使用 PHP PCRE 时遇到问题,而且我习惯了 POSIX,所以我不太确定我做错了什么。基本上,此函数最多匹配 10 个以逗号分隔的数字。然而,它也匹配字符串 sdf (可能还有许多其他字符串),我不明白原因。谁能帮助我吗?

$pattern='^\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?^';

$leftcheck=preg_match($pattern, $leftmodules);
$centercheck=preg_match($pattern, $centermodules);
$rightcheck=preg_match($pattern, $rightmodules);

if(!$leftcheck OR !$centercheck OR !$rightcheck)
{
$editpage = $_SERVER['HTTP_REFERER'].'?&error=1';
die("Location:$editpage");
}

I'm having a problem with PHP PCRE, and I'm used to POSIX, so I'm not too sure about what I'm doing wrong. Basically, this function is matching up to 10 numbers separated by commas. However, it's also matching the string sdf (and probably many others), which I don't see the reason for. Can anyone help me?

$pattern='^\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?\d{0,5},? ?^';

$leftcheck=preg_match($pattern, $leftmodules);
$centercheck=preg_match($pattern, $centermodules);
$rightcheck=preg_match($pattern, $rightmodules);

if(!$leftcheck OR !$centercheck OR !$rightcheck)
{
$editpage = $_SERVER['HTTP_REFERER'].'?&error=1';
die("Location:$editpage");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

诗酒趁年少 2024-08-14 21:26:12

我假设如下:

  • 空格可能存在,也可能不存在。
  • 数字可以是任意长度。
  • 只允许使用数字、空格和逗号。
  • 允许使用尾随逗号,且后面不带数字。
  • 1 到 10 之间的数字用逗号分隔即可。

鉴于此:

$pattern = '/^(\d+,* *){1,10}$/';

有效。

I'm assuming the following:

  • Spaces may or may not be there.
  • Numbers can be any length.
  • Only numbers, spaces, and comma's are allowed.
  • Trailing commas without a number after them are allowed.
  • Between 1 and 10 numbers seperated by commas are ok.

Given that:

$pattern = '/^(\d+,* *){1,10}$/';

works.

慕巷 2024-08-14 21:26:12

据我所知,您提供的正则表达式将与您传递给它的任何内容匹配。这就是为什么

\d{0,5}     #\d matches any digit character, while {0,5} means the
            #preceding character must be repeated between **0** and five times

你的正则表达式本质上是短路的。引擎看到字符串的第一个字符并说“某个数字重复了 0 次吗?是吗?好的,这是一个匹配!”

From what I can see, the regular expression you provided will match anything you pass into it. Here's why

\d{0,5}     #\d matches any digit character, while {0,5} means the
            #preceding character must be repeated between **0** and five times

So your regular expression is essentially short circuiting. The engine see the first character of your string and says "has a digit been repeated 0 times? Yes? OK, it's a match!

っ〆星空下的拥抱 2024-08-14 21:26:12

我认为如果你的号码仅用逗号分隔,类似这样的事情应该可以做到

$pattern = '^\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5}
;

I think if your number are only separated by commas something like this should do it

$pattern = '^\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5},\d{0,5}
;
沫离伤花 2024-08-14 21:26:12

您需要将模式包含在两个相等的符号之间才能使其有效。人们通常使用/。

$pattern = '/some pattern/';

为了匹配整个内容,您希望在开头添加 ^,在结尾添加 $。出现这个错误可能就是您的 sdf 匹配的原因。

$pattern = '/^whole pattern match$/';

如何分隔数字有点令人困惑。是逗号还是空格?两者都可以吗?没有呢?不过,这是我最好的猜测。

$pattern = '/^\d{,5}[, ](\d{,5}[, ]){,9}$/';

You need to contain the pattern between two equal symbols for it to be valid. People usually use /.

$pattern = '/some pattern/';

To match the whole thing you want to have ^ at the start and $ at the end. Getting this wrong is probably why your sdf was matching.

$pattern = '/^whole pattern match$/';

It's a bit confusing how the numbers will be separated. Is it comma or space? Is both OK? What about none? Here's my best guess though.

$pattern = '/^\d{,5}[, ](\d{,5}[, ]){,9}$/';
潦草背影 2024-08-14 21:26:11
^\d{1,5}(, *\d{1,5}){0,9}$
^\d{1,5}(, *\d{1,5}){0,9}$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文