Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正则表达式使用preg_除以一个点(.)_Php_Regex - Fatal编程技术网

Php 正则表达式使用preg_除以一个点(.)

Php 正则表达式使用preg_除以一个点(.),php,regex,Php,Regex,你好,我需要用“.”分割一个字符串 拥有: 与: 我得到: array(7) { [0]=> string(22) "Step one (30 min aprox" [1]=> string(1) ")" [2]=> string(8) "Step two" [3]=> string(14) "Step three $12" [4]=> string(2) "00" [5]=> string(13) " Step

你好,我需要用“.”分割一个字符串 拥有:

与:

我得到:

array(7) {
  [0]=>
  string(22) "Step one (30 min aprox"
  [1]=>
  string(1) ")"
  [2]=>
  string(8) "Step two"
  [3]=>
  string(14) "Step three $12"
  [4]=>
  string(2) "00"
  [5]=>
  string(13) " Step four ud"
  [6]=>
  string(7) " visit "
}
我只想要这个

[0] Step one (30 min aprox.)
[1] Step two
[2] Step three $12.00
[3] Step four ud. visit
前一个字符(在“.”之前)不能是数字或特殊字符,下一个字符必须是大写字母

注意:步骤*只是一个示例

希望能帮上忙,谢谢

好的,再试一次:

$array = preg_split (/\.\s?Step/, $string);
$l = count ($array);
for ($i=1; $i<$l; $i++) {
    $array [$i] = 'Step' . $array [$i];
}
$array=preg\u split(/\.\s?Step/,$string);
$l=计数($数组);

对于($i=1;$i为您的字符串提供了一个更好的
preg_-split

$string = "Step one (30 min aprox.).Step two.Step three $12.00. Step four ud. visit ";
print_r( preg_split('/(?<=[^0-9])[.](?<![0-9])/', $string, 0));

Array ( 
 [0] => Step one (30 min aprox 
 [1] => ) 
 [2] => Step two
 [3] => Step three $12.00. Step four ud 
 [4] => visit )
$string=“第一步(大约30分钟)。第二步。第三步$12.00。第四步ud.visit”;

打印(preg__)split('/(?)
以获得所需的确切句子结构。

如果需要更一般的情况,这可能会有所帮助

$string = preg_replace("/\.\s?([A-Z])/", "*****$1", $string);
$array = explode("*****", $string);
我还没有测试过它,但我认为它可以满足您的需要。

您也可以使用。请尝试以下模式:

/\.(?=(\s*)Step)/
集成到代码中,如下所示:

// Split on periods that are followed possibly by 0+ spaces, and the word 'Step'
preg_split('/\.(?=(\s*)Step)/', $string) 
输出:

Array
(
    [0] => Step one (30 min aprox.)
    [1] => Step two
    [2] => Step three $12.00
    [3] =>  Step four ud. visit 
)

这似乎适用于您的示例字符串

$string = "Step one (30 min aprox.).Step two.Step three $12.00. Step four ud. visit ";

preg_match_all("/(Step.*?)(\.(?=\s*Step)|$)/",$string,$matches);

foreach ($matches[1] as $m) {
    echo $m,"\n";
}

该模式查找以Step开头的字符串,该字符串以“.”字符后跟Step(前视)或字符串结尾。

您声明“上一个字符和下一个字符不能是数字或特殊字符,必须是大写字母”,但在您的示例中,上一个字符不是大写字母。我提供了一个答案,使用正向前瞻对当前正则表达式进行了微调。这将按预期分隔步骤。@Madbreaks抱歉,我已经修复了“下一次聊天必须是大写字母”如果你想得到更好的答案,你需要提供更好的示例数据谢谢你@PenguinCoder,但我得到的是
array(5){[0]=>string(22)“第一步(30分钟大约”[1]=>string(1)””””[2]=>string(8)“第二步”[3]=>string(31)”第三步$12.00。第四步ud“[4]=>string(7)”访问“}
@Kas3这是因为匹配拆分的方式,即对
句点字符进行拆分。不幸的是,如果没有更扩展的正则表达式来检查单数字,我的方法将始终在没有数字环绕的任何句点上拆分。另外,请阅读我在回答中关于
问题。我知道正则表达式不能提供您要求的确切结构,但没有更好的输入约束,输出也有限。谢谢您它的工作原理很好,我的示例将尝试实际案例!它不会总是一个步骤$string只是一个示例,但也谢谢!谢谢!但是$string包含“步骤”这只是一个例子
Array
(
    [0] => Step one (30 min aprox.)
    [1] => Step two
    [2] => Step three $12.00
    [3] =>  Step four ud. visit 
)
$string = "Step one (30 min aprox.).Step two.Step three $12.00. Step four ud. visit ";

preg_match_all("/(Step.*?)(\.(?=\s*Step)|$)/",$string,$matches);

foreach ($matches[1] as $m) {
    echo $m,"\n";
}