如果你想在windows / office上打印任意页面,你可以像在图片中那样定义:
因此,这将打印页面:1,2,3,6,7,8
现在,我正在尝试使用Regex做同样的事情
<?php
$str = "1-4,6,7,8";
preg_match('/((\d+-\d+)|(\d+)),((\d+-\d+)|(\d+))/',$str,$out);
print_r($out);
?>
并打印
Array ( [0] => 1-4,6 [1] => 1-4 [2] => 1-4 [3] => [4] => 6 [5] => [6] => 6 )
但我希望是以下
Array ( [0] => 1-4 [1] => 6, [2] => 7, [3] => 7 )
我该怎么做?
以下是fiddle
答案 0 :(得分:3)
请检查此正则表达式模式
$str = "1-4,6,7,8";
preg_match('/((\d+-\d+)|(\d+)),?/',$str,$out);
print_r($out);
或更好地使用explode
功能:
$str = "1-4,6,7,8";
$out = explode(',', $str);
print_r($out);
答案 1 :(得分:0)
这应该可以解决问题:
(\d+)-?(\d*)?(,(?!$))?
答案 2 :(得分:0)
使用此:
$str = "1-4,6,7,8";
preg_match_all('/(\d+(?:-\d+)?),?/', $str, $out);
print_r($out);
<强>输出:强>
Array
(
[0] => Array
(
[0] => 1-4,
[1] => 6,
[2] => 7,
[3] => 8
)
[1] => Array
(
[0] => 1-4
[1] => 6
[2] => 7
[3] => 8
)
)