简体   繁体   English

PHP中带逗号的正则表达式

[英]Regular expression in PHP with comma

I'm trying to extract mark-up from a string. 我正在尝试从字符串中提取标记。 I'm part way there but am having trouble with commas in my pattern. 我在其中,但我的模式遇到逗号问题。

Take this example input string : 以以下示例输入字符串为例:

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

I wish to extract the tags including the attribute 'index'. 我希望提取包括属性“ index”的标签。

I need both variations, ie patterns without and with commas in the attribute. 我需要两种变体,即属性中不带逗号的模式。

If I do this : 如果我这样做:

$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 );

The array $thats only contains this : 数组$ thats仅包含以下内容:

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

)

So clearly I am doing something wrong with how I use the comma in the pattern, because it omits those with commas. 显然,我在模式中使用逗号的方式有误,因为它忽略了那些逗号。

Could someone please advise. 有人可以建议。 Thank you. 谢谢。

Add + after the character class to match one or more characters in the given list. 在字符类之后添加+以匹配给定列表中的一个或多个字符。

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

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM