正则表达式匹配链接没有< a>标签 [英] regex matching links without <a> tag

查看:178
本文介绍了正则表达式匹配链接没有< a>标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(http([s]?):\/\/?)(([a-zA-Z0-9]+(\.?))+)([a-zA-Z0-9]+((\.[a-zA-Z]{2,5}){1,2})((\/[a-zA-Z0-9\?&=_\-\~:/?#[\]@!\$&'()\*\+,;]*)*)((\.[a-zA-Z]{2,5}){0,2}))

这是我的正则表达式这适用于匹配字符串中的链接。但我不希望它选择每个链接。如果链接之前有> ,或者< / a> 之后,该链接不应该算术怎么办?

This is my regex which is working well for matching the links in the string. But I don't want it to select every link. If a link has "> before it, or </a> after it, that link shouldn't be mathced. How can it be done?

这些应匹配:

adasdas http://www.stackoverflow.com asdasas
adasdasahttp://www.stackoverflow.com/something asdas

这些不应匹配:

adasdas<a href="somelink">           http://www.stackoverflow.com     </a>asdasas
adasdasa<a href="somelink">http://www.stackoverflow.com/something</a> asdas

为什么我需要这个?:我希望每个链接都是可点击的,即使它不在锚标签之间。

Why do I need this?: I want every link to be clickable even if it isn't between anchor tags.

推荐答案

通过所有有关使用正则表达式来解析HTML,如果你想使用正则表达式该任务的免责声明,这将工作:

With all the disclaimers about using regex to parse html, if you want to use regex for this task, this will work:

$regex="~<a.*?</a>(*SKIP)(*F)|http://\S+~";

S ee 演示

这个问题是这个问题中解释的技术的经典案例,以正则表达式匹配模式,不包括......

This problem is a classic case of the technique explained in this question to "regex-match a pattern, excluding..."

交替的左侧 | 匹配完整的< a ... tags< / a> 然后故意失败,之后引擎跳到字符串中的下一个位置。右侧与网址匹配,我们知道它们是正确的,因为它们与左侧的表达不匹配。

The left side of the alternation | matches complete <a ...tags </a> then deliberately fails, after which the engine skips to the next position in the string. The right side matches the urls, and we know they are the right ones because they were not matched by the expression on the left.

我放在右边的url正则表达式可以精炼,只需使用适合你需要的任何东西。

The url regex I put on the right and can be refined, just use whatever suits your needs.

参考

  • How to match (or replace) a pattern except in situations s1, s2, s3...
  • Article about matching a pattern unless...

这篇关于正则表达式匹配链接没有&lt; a&gt;标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆