[英]How to write this PHP regex?
I have a string of numbers surrounded by parenthesis: 我有一串数字用括号括起来:
(2)(56)(9)(12)(2)
I was given the following regex by someone to split the numbers out of their parentheses: 有人给我以下正则表达式,以便将数字从括号中分开:
<?php
$string = '(2)(56)(9)(12)(2)';
$pattern = "/\\)\\(|\\(|\\)?/";
$numbers = preg_split($pattern, $string);
foreach ($numbers as $number) {
echo $number.'<br />';
}
?>
This outputs: 输出:
<br />
<br />
2<br />
<br />
5<br />
6<br />
<br />
9<br />
<br />
1<br />
2<br />
<br />
2<br />
<br />
<br />
When I really need it to output: 当我真的需要它输出时:
2<br />
56<br />
9<br />
12<br />
2<br />
How can the regex be altered to make it work? 如何修改正则表达式以使其正常工作?
ps each number in between the parenthesis can be of an unlimited length. ps括号之间的每个数字可以无限长。
I would not use preg_split in the first place. 我不会一开始就使用preg_split。 If that's really your source string, then look just for numbers:
如果这确实是您的源字符串,则仅查找数字:
preg_match_all("/\d+/", $string, $numbers);
print_r($numbers[0]);
First, there's an error in the regex. 首先,正则表达式中存在错误。 It should be:
它应该是:
$pattern = "/\\)\\(|\\(|\\)/";
Note the last ?
注意最后
?
is removed. 已移除。
The easy answer is to just check for empty numbers: 简单的答案是只检查空数字:
foreach ($numbers as $number) {
if ($number !== '') {
echo $number.'<br />';
}
}
Note the strict !==
test since 0
may be a valid number... 请注意严格的
!==
测试,因为0
可能是有效数字...
Or, add the PREG_SPLIT_NO_EMPTY
option to the preg_split
call: 或者,将
PREG_SPLIT_NO_EMPTY
选项添加到preg_split
调用中:
$numbers = preg_split($pattern, $string, -1, PREG_SPLIT_NO_EMPTY);
Another option is to use preg_match_all
with the simple regex: /\\d+/
: 另一个选择是将
preg_match_all
与简单的正则表达式一起使用: /\\d+/
:
$matches = array();
preg_match_all('/\d+/', $string, $matches);
$numbers = $matches[0];
Use preg_match
instead of split and use /\\\\d+/
as your expression 使用
preg_match
而不是split并使用/\\\\d+/
作为表达式
Try 尝试
$string = '(2)(56)(9)(12)(2)';
$pattern = "/\d+/";
preg_match_all($pattern, $string, $numbers);
foreach ($numbers as $number) {
echo $number.'<br />';
}
I'm not sure on PHP regex syntax either \\\\d
or \\d
我不确定
\\\\d
或\\d
是PHP regex语法
You just need the PREG_SPLIT_NO_EMPTY option of preg_split, and a small change in the pattern: 您只需要preg_split的PREG_SPLIT_NO_EMPTY选项,并在模式中进行一些小的更改:
$pattern = "/\\)\\(|\\(|\\)/"; // removed the "?"
$numbers = preg_split($pattern, $string, -1, PREG_SPLIT_NO_EMPTY); // added PREG_SPLIT_NO_EMPTY
Or use preg_match: 或使用preg_match:
$string = '(2)(56)(9)(12)(2)';
preg_match_all('#\d+#', $string, $matches);
foreach ($matches[0] as $number) {
echo "$number<br />\n";
}
Output: 输出:
2<br />
56<br />
9<br />
12<br />
2<br />
Pass the flag PREG_SPLIT_NO_EMPTY
to not return empty tokens and remove the ?
传递标志
PREG_SPLIT_NO_EMPTY
以不返回空令牌并删除?
which allows for an empty separator. 这允许一个空的分隔符。 You also don't need to double-escape the
()
's in PHP. 您也不需要在PHP中对
()
进行两次转义。 Using preg_match
on (\\d+)
looks like it would be a simpler solution to your problem though. 在
(\\d+)
上使用preg_match
看起来似乎可以解决您的问题。
$string = '(2)(56)(9)(12)(2)';
$pattern = "/\)\(|\(|\)/";
$numbers = preg_split($pattern, $string, -1, PREG_SPLIT_NO_EMPTY);
foreach ($numbers as $number) {
echo "$number<br />\n";
}
Output: 输出:
2<br />
56<br />
9<br />
12<br />
2<br />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.