尝试使用 reg ex 获取基本 url
我需要匹配“基本”url,我的意思是:
Not match --> http://google.com
Not match --> http://www.google.com
Not match --> www.google.com
Match! --> google.com
我试图使用反向查找来确保没有 http:// 或 www,但它似乎无法正常工作。
I need to match the "base" url, what I mean is:
Not match --> http://google.com
Not match --> http://www.google.com
Not match --> www.google.com
Match! --> google.com
I was trying to use a negative look behind to make sure there was no http:// or www, but it didn't seem to work correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这必须仅使用一个正则表达式吗?
您可以使用第一个与找到的所有 URL 相匹配的正则表达式。像这样的事情:
然后过滤所有匹配项并保留与以下内容不匹配的匹配项:
虽然说实话,我不会使用正则表达式,除非这是绝对必要的。
注意:
您始终可以找到更好的正则表达式来匹配 URL。这里的问题是它们可能不以
http://
或www
开头,因此我们不能对正则表达式限制太多。准备好其他根本不是 URL 的匹配,例如:I was there Saturday.but no one saw me
中的yesterday.but
Do this has to be with only one regex?
You could have the first regex that will match all URLs found. Something like that:
And then filter all matches and keep the ones that do not match the following:
although to be honest, I wouldn't use Regex unless it is strictly necessary for that.
Note:
You can always find a better regex to match the URLs. The thing here is that they may not start with
http://
orwww
, so we can't restrict the regex so much. Be ready to have other matches that are not urls at all, like:yesterday.but
inI was there yesterday.but no one saw me