亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

正則表達式 (PHP) 匹配從字符串開頭開始或前面有空格+數字+/+空格的所有內容

正則表達式 (PHP) 匹配從字符串開頭開始或前面有空格+數字+/+空格的所有內容

PHP
鴻蒙傳說 2023-05-26 14:13:39
我有一個看起來像這樣的字符串:1/ This is a string and it                                                                                   has 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 line 2020/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 \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 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 letters721/ And this is the last line ';preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);// Print the entire match resultvar_dump($matches);這是一個演示但是這里有問題:如果兩個匹配項在同一行上,它將不會捕獲字符串(匹配 4 和 5 只會捕獲匹配 4)它不捕獲新行上的字符串它不會捕獲字符串中有數字后跟 / 的部分,例如2020/xyz followed by some letters
查看完整描述

2 回答

?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

$將與行尾匹配的錨點(使用 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 修飾符。


查看完整回答
反對 回復 2023-05-26
?
呼啦一陣風

TA貢獻1802條經驗 獲得超6個贊

嘗試:

(?:\s|^)\d{1,3}\/\s(?:(?!\s\d{1,3}\/\s)[\s\S])*

請參閱正則表達式演示

<?php

$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

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";


preg_match_all('/(?:\s|^)\d{1,3}\/\s(?:(?!\s\d{1,3}\/\s)[\s\S])*/', $str, $matches, PREG_SET_ORDER);

print_r($matches);

印刷:


Array

(

    [0] => Array

        (

            [0] => 1/ This is a string and it

has some text on a new line

        )


    [1] => Array

        (

            [0] =>

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

        )


    [2] => Array

        (

            [0] =>

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

2020/xyz followed by some letters

        )


    [3] => Array

        (

            [0] =>

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

        )


    [4] => Array

        (

            [0] =>  721/ And this is the last line

        )


)


查看完整回答
反對 回復 2023-05-26
  • 2 回答
  • 0 關注
  • 325 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號