正则表达式运算符或 - preg_match_all
regex operator OR - preg_match_all
我有字符串 {tender id="2,3"}
或 {tender catid="2" id="2,3"}
或 {tender catid="2"}
我用
$regex = "/{tender ((.*?id\s*=\s*['\"](.*?)['\"]) (.*?id\s*=\s*['\"](.*?)['\"]))}/";
但只有当我在字符串中同时包含 id 和 catid 时才能工作....如果字符串中只有一个参数,我如何编写正则表达式才能工作?
尝试:{tender (?:cat)?id="(\d+,?)+"}
如果它不适合您,您可能需要指定其他详细信息。正如您所要求的,只有当数据结构中有 catid
或 id
时才会匹配(示例中的情况 1 和 3)。
说明
{tender
数据的开始。
(?:cat)?
如果可以的话赶上 cat
id="
获取 id 的开头
(\d+,?)+
获取可能后跟或不跟逗号的数字
"}
数据结束。
我有字符串 {tender id="2,3"}
或 {tender catid="2" id="2,3"}
或 {tender catid="2"}
我用
$regex = "/{tender ((.*?id\s*=\s*['\"](.*?)['\"]) (.*?id\s*=\s*['\"](.*?)['\"]))}/";
但只有当我在字符串中同时包含 id 和 catid 时才能工作....如果字符串中只有一个参数,我如何编写正则表达式才能工作?
尝试:{tender (?:cat)?id="(\d+,?)+"}
如果它不适合您,您可能需要指定其他详细信息。正如您所要求的,只有当数据结构中有 catid
或 id
时才会匹配(示例中的情况 1 和 3)。
说明
{tender
数据的开始。(?:cat)?
如果可以的话赶上cat
id="
获取 id 的开头(\d+,?)+
获取可能后跟或不跟逗号的数字"}
数据结束。