正则表达式 (PHP) 匹配从字符串开头开始或前面有空格+数字+/+空格的所有内容

我有一个看起来像这样的字符串:


1/ This is a string and it                                                                                   

has some text on a new line

2/ And then there's another string that has text only on one line

532/ Another string that has some year on a new line 

2020/xyz followed by some letters

720/ This is a match on the same line with another match but the other match won't be captured 721/ And this is the last line 

\d我想捕获以小于或等于 3 dgits long( ) 的数字 ( {1,3}) 开头并具有正斜杠 ( /) 并且位于字符串开头或前后有空格或换行符的每个字符串( \s+).


这就是我希望它看起来像的样子:


[Match 1] 1/ This is a string and it has some text on a new line

[Match 2] 2/ And then there's another string that has text only on one line

[Match 3] 532/ Another string that has some year on a new line 2020/xyz followed by some letters

[Match 4] 720/ This is a match on the same line with another match but the other match won't be captured

[Match 5] 721/ And this is the last line 

到目前为止,这是我的代码:


$re = '/(\s|^)(?s)\d{1,3}+\/+\s+.*?(?=\d+\/+\s+|$)/m';

$str = '1/ This is a string and it 

has some text on a new line

2/ And then there\'s another string that has text only on one line

532/ Another string that has some year on a new line

2020/xyz followed by some letters

721/ And this is the last line ';


preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);


// Print the entire match result

var_dump($matches);

这是一个演示

但是这里有问题:

  1. 如果两个匹配项在同一行上,它将不会捕获字符串(匹配 4 和 5 只会捕获匹配 4)

  2. 它不捕获新行上的字符串

  3. 它不会捕获字符串中有数字后跟 / 的部分,例如2020/xyz followed by some letters


鸿蒙传说
浏览 265回答 2
2回答

泛舟湖上清波郎朗

$将与行尾匹配的锚点(使用 m 修饰符)更改为\z锚点(无论修饰符如何匹配字符串的末尾)。这样,不情愿的量词.*?将能够匹配多行,而不是停在行的第一端。要在同一行中查找多个匹配项,请\s+在数字前添加前瞻。否则数字之前的空间不能被消耗两次(一次被.*?一次被 消耗一次(\s|^))。~(\s|^)\d{1,3}/+\s.*?(?=\s+\d{1,3}/+\s|\z)~ms请注意,您可以使用以下方法获得修剪结果:~(?<!\S)\d{1,3}/+\s.*?(?=\s+\d{1,3}/+\s|\s*\z)~s要减少步骤数,您可以更改\s.*?并(?>\s+\S+)*?删除不再需要的 s 修饰符。

呼啦一阵风

尝试:(?:\s|^)\d{1,3}\/\s(?:(?!\s\d{1,3}\/\s)[\s\S])*请参阅正则表达式演示<?php$str = "1/ This is a string and ithas some text on a new line2/ And then there's another string that has text only on one line532/ Another string that has some year on a new line2020/xyz followed by some letters720/ This is a match on the same line with another match but the other match won't be captured 721/ And this is the last line";preg_match_all('/(?:\s|^)\d{1,3}\/\s(?:(?!\s\d{1,3}\/\s)[\s\S])*/', $str, $matches, PREG_SET_ORDER);print_r($matches);印刷:Array(&nbsp; &nbsp; [0] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => 1/ This is a string and ithas some text on a new line&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [1] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] =>2/ And then there's another string that has text only on one line&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [2] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] =>532/ Another string that has some year on a new line2020/xyz followed by some letters&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [3] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] =>720/ This is a match on the same line with another match but the other match won't be captured&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [4] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] =>&nbsp; 721/ And this is the last line&nbsp; &nbsp; &nbsp; &nbsp; ))
打开App,查看更多内容
随时随地看视频慕课网APP