【发布时间】:2018-06-04 17:43:34
【问题描述】:
我正在寻找 PHP 中的正则表达式来提取链接文本,该链接包含锚文本中的特定单词(苹果、家庭、汽车)。
重要提示:事先不知道链接的格式。
例如:
<a href="fruit.html">The Apple red</a>
<a href="Construction.html#one">The big Home</a>
<a href="automotive.html?lang=en">Car for rent</a>
想要的结果:
fruit.html
Construction.html#one
automotive.html?lang=en
我的模式:
/<a.*?href="(.*)".*?>apple|car|home<\/a>/i
更新:这种模式有效
'/<a.+href=["\'](.*)["\'].*>(.*(?:apple|car|home).*)<\/a>/iU'
【问题讨论】:
-
我在 RegEx 上很糟糕,但这里是你的起点。
[^<]*(<a href="([^"]+)">([^<]+)<\/a>)。Group 1: String、Group 2: Href、Group 3: Text。您真正需要做的就是弄清楚如何multiline 匹配以及如何比较 与Group 3。 Regex101 Link -
@Alex,谢谢你的帮助
标签: php regex regex-lookarounds