正则表达式查找连续整数
我很难让我的正则表达式代码在 PHP 中正常工作。这是我的代码:
$array = array(); // Used to satisfy the 3rd argument requirment of preg_match_all.
$regex = '/(012|345|678|987|654|321|123|456|789|876|543|210|234|567|765|432)/';
$subject = '123456';
echo preg_match_all($regex, $subject, $array).'<br />';
print_r($array);
运行此代码时,它将输出:
2
Array
(
[0] => Array
(
[0] => 123
[1] => 456
)
[1] => Array
(
[0] => 123
[1] => 456
)
)
我该怎么做才能匹配 123、234、345 和 456?
提前致谢!
I am having a difficult time getting my regular expression code to work properly in PHP. Here is my code:
$array = array(); // Used to satisfy the 3rd argument requirment of preg_match_all.
$regex = '/(012|345|678|987|654|321|123|456|789|876|543|210|234|567|765|432)/';
$subject = '123456';
echo preg_match_all($regex, $subject, $array).'<br />';
print_r($array);
When this code is ran it will output:
2
Array
(
[0] => Array
(
[0] => 123
[1] => 456
)
[1] => Array
(
[0] => 123
[1] => 456
)
)
What can I do so that it will match 123, 234, 345 and 456?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正则表达式不是这项工作的正确工具(它不会返回“子匹配”)。只需在循环中使用
strpos
即可。Regex is not the right tool for this job (it's not going to return "sub-matches"). Simply use
strpos
in a loop.输出:
您正在尝试检索主题字符串中相互重叠的匹配项,这通常是不可能的。但是,在许多情况下,您可以通过将整个正则表达式包装在捕获组中,然后将其包装在前瞻中来伪造它。由于前瞻在匹配时不会消耗任何字符,因此正则表达式引擎在每次成功匹配后都会手动向前移动一个位置,以避免陷入无限循环。但捕获组仍然有效,因此您可以按照通常的方式检索捕获的文本。
请注意,我只打印了第一个捕获组 (
$array[1]
) 的内容。如果我打印了整个数组($array
),它看起来会像这样:在 ideone 上查看它的实际应用
output:
You're trying to retrieve matches that overlap each other in the subject string, which in general is not possible. However, in many cases you can fake it by wrapping the whole regex in a capturing group, then wrapping that in a lookahead. Because the lookahead doesn't consume any characters when it matches, the regex engine manually bumps forward one position after each successful match, to avoid getting stuck in an infinite loop. But capturing groups still work, so you can retrieve the captured text in the usual way.
Notice that I only printed the contents of the first capturing group (
$array[1]
). If I had printed the whole array of arrays ($array
), it would have looked like this:see it in action on ideone
可以用正则表达式来完成。原始代码的问题是,一旦发生匹配,字符就会被消耗,并且正则表达式不会回溯。这是一种方法:
但是,正如另一个答案中所建议的,正则表达式可能不是在这种情况下使用的正确工具,具体取决于您的更大目标。此外,上面的代码可能不是使用正则表达式解决此问题的最有效方法(wrt 内存或wrt 性能)。这只是一个简单的满足要求的解决方案。
It can be done with regular expressions. The problem with your original code is that as soon as a match occurs, the character is consumed and the regular expression will not backtrack. Here's one way to do it:
As suggested in another answer, however, regular expressions might not be the correct tool to use in this situation, depending on your larger goal. In addition, the above code may not be the most efficient way (wrt memory or wrt performance) to solve this with regular expressions. It's just a striaghtforward fulfill-the-requirement solution.
是的,这是一个黑客,但你可以使用正则表达式
返回:
注意:这仅适用于并发数字
Yeah it's a hack but you can use RegEx
Returns:
NOTE: This would only work with concurrent numbers