第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

正則表達(dá)式 (PHP) 匹配從字符串開(kāi)頭開(kāi)始或前面有空格+數(shù)字+/+空格的所有內(nèi)容

正則表達(dá)式 (PHP) 匹配從字符串開(kāi)頭開(kāi)始或前面有空格+數(shù)字+/+空格的所有內(nèi)容

PHP
鴻蒙傳說(shuō) 2023-05-26 14:13:39
我有一個(gè)看起來(lái)像這樣的字符串: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( ) 的數(shù)字 ( {1,3}) 開(kāi)頭并具有正斜杠 ( /) 并且位于字符串開(kāi)頭或前后有空格或換行符的每個(gè)字符串( \s+).這就是我希望它看起來(lái)像的樣子:[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);這是一個(gè)演示但是這里有問(wèn)題:如果兩個(gè)匹配項(xiàng)在同一行上,它將不會(huì)捕獲字符串(匹配 4 和 5 只會(huì)捕獲匹配 4)它不捕獲新行上的字符串它不會(huì)捕獲字符串中有數(shù)字后跟 / 的部分,例如2020/xyz followed by some letters
查看完整描述

2 回答

?
泛舟湖上清波郎朗

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊

$將與行尾匹配的錨點(diǎn)(使用 m 修飾符)更改為\z錨點(diǎn)(無(wú)論修飾符如何匹配字符串的末尾)。

這樣,不情愿的量詞.*?將能夠匹配多行,而不是停在行的第一端。

要在同一行中查找多個(gè)匹配項(xiàng),請(qǐng)\s+在數(shù)字前添加前瞻。否則數(shù)字之前的空間不能被消耗兩次(一次被.*?一次被 消耗一次(\s|^))。

~(\s|^)\d{1,3}/+\s.*?(?=\s+\d{1,3}/+\s|\z)~ms

請(qǐng)注意,您可以使用以下方法獲得修剪結(jié)果:

~(?<!\S)\d{1,3}/+\s.*?(?=\s+\d{1,3}/+\s|\s*\z)~s

要減少步驟數(shù),您可以更改\s.*?(?>\s+\S+)*?刪除不再需要的 s 修飾符。


查看完整回答
反對(duì) 回復(fù) 2023-05-26
?
呼啦一陣風(fēng)

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超6個(gè)贊

嘗試:

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

請(qǐng)參閱正則表達(dá)式演示

<?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

        )


)


查看完整回答
反對(duì) 回復(fù) 2023-05-26
  • 2 回答
  • 0 關(guān)注
  • 349 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)