我有一个看起来像这样的字符串:
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);
这是一个演示
但是这里有问题:
如果两个匹配项在同一行上,它将不会捕获字符串(匹配 4 和 5 只会捕获匹配 4)
它不捕获新行上的字符串
它不会捕获字符串中有数字后跟 / 的部分,例如2020/xyz followed by some letters
泛舟湖上清波郎朗
呼啦一阵风