[英]Regexp: |(OR) condition doesn't work
Can't figure out why OR condition in my regexp isn't working. 无法弄清楚为什么我的正则表达式中的OR条件不起作用。
$str = " <li><a href=\"\" class=\"gateway-image\"><img src=\"<?=theme_url()?>/images/Cherry Credits.jpg\" alt=\"\"/></a></li>
<li><a href=\"\" class=\"gateway-image\"><img src=\"<?=theme_url()?>/images/gudangvoucher.jpeg\" alt=\"\" style=\"width: 140px!important\"/></a></li>";
$regexp = '/^(.*\<li\>\<a href\=)(.*cherry|credits.*)$/im';
preg_match($regexp, $str, $m);
It seems regexp is correct but it can't find string Cherry Credits.jpg
as I expected. 似乎regexp是正确的,但是找不到我期望的字符串Cherry Credits.jpg
。 What is wrong? 怎么了?
You've written the expression using the start- and end-of-line anchors ^
and $
. 您已经使用行首和结尾锚^
和$
编写了表达式。
This doesn't match because your string continues on after cherry
and there are additional characters between href=
and credits
. 这不匹配,因为您的字符串在cherry
之后继续, 并且在href=
和credits
之间还有其他字符。
You can either remove the line anchors: 您可以删除行锚:
(.*\<li\>\<a href\=)(.*cherry|credits.*)
Or you can move the trailing .*
(and probably the leading .*
) outside of the capture group: 或者,您可以将尾随.*
(可能还有前导.*
)移到捕获组之外:
^(.*\<li\>\<a href\=).*(cherry|credits).*$
You need to remove the "$" sign at the end, because it will expect a end of line. 您需要在结尾处删除“ $”符号,因为它将在行尾出现。
Try this: 尝试这个:
^(.*\\<li\\>\\<a href\\=).*(cherry|credits)(\\.jpg)
You can use online testers like this site: https://regex101.com/ and check if you are doing OK. 您可以使用以下站点这样的在线测试仪: https : //regex101.com/并检查您的操作是否正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.