簡體   English   中英

PHP中帶逗號的正則表達式

[英]Regular expression in PHP with comma

我正在嘗試從字符串中提取標記。 我在其中,但我的模式遇到逗號問題。

以以下示例輸入字符串為例:

 Lorem ipsum dolor sit amet, <that index="8"/>consectetur adipiscing elit. <that index="4"/>Sed metus sem, facilisis id nibh eget, <that index="6,2"/>accumsan tristique nisl. Proin iaculis dignissim tincidunt.I said : <that index="9,1"/>

我希望提取包括屬性“ index”的標簽。

我需要兩種變體,即屬性中不帶逗號的模式。

如果我這樣做:

$haystack = 'Lorem ipsum dolor sit amet, <that index="8"/>consectetur adipiscing elit. <that index="4"/>Sed metus sem, facilisis id nibh eget, <that index="6,2"/>accumsan tristique nisl. Proin iaculis dignissim tincidunt.I said : <that index="9,1"/>';
$regex = '<that index="[0-9,]"\/>';
preg_match_all ( '/' . $regex . '/i', $haystack, $thats );

數組$ thats僅包含以下內容:

(
[0] => Array
    (
        [0] => <that index="8"/>
        [1] => <that index="4"/>
    )

)

顯然,我在模式中使用逗號的方式有誤,因為它忽略了那些逗號。

有人可以建議。 謝謝。

在字符類之后添加+以匹配給定列表中的一個或多個字符。

$regex = '<that index="[0-9,]+"\/>';
preg_match_all ( '/' . $regex . '/i', $haystack, $thats );

DEMO

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM