我设法从该字符串中筛选出所有URL:
hi, this is your link (but this one is bad formatted and useless):
https://www.test.comhttps://app.test.com/a/b/c/5e20bed422e7880012ba8acc/next?param=1?locale=2
but there is a good link too:
https://app.test.com/a/b/c/5e20bed422e7880012ba8acc/next?param=1?locale=2
and there are also other irrelevant links:
http://www.google.com
http://test.test.com
用这个正则表达式
http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+
在玩了一段时间后,我一直在试图弄清楚如何调整它,只得到:
https://www.test.com/a/b/c/5e20bed422e7880012ba8acc/next?param=1?locale=2
过滤出包含“/next”但不包含“comhttps”的URL的最简单方法是什么
非常感谢
像这样的怎么样
我们将使用
i
标志进行不区分大小写的搜索在这里测试:https://regex101.com/r/J62XZq/2
说明
https?:\/\/
是查找http://或https://[a-z0-9]+(?:[\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}
是查找有效的域名。我没有检查它是否真的是完全防弹的。但这似乎并不坏。我们也许可以找到一个官方的正则表达式来验证域名。(?:)
组是一个非捕获组(如果我们不需要它)(https?:\/\/[a-z0-9]+(?:[\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6})
同时在一起并在一个组中捕获,因此我们拥有原始URL(?=https?)
是一个正向前瞻,因此前面的域后面必须跟有http或https。如果可以使用ftp或其他协议,则可能需要对其进行调整(\S+)
是将非空格匹配一次或多次,并将其捕获到一个组中(以供以后使用和处理。必须处理第二个组,以便除去第二个查询字符串?param=x&option
,该字符串可能属于周围的URL编辑
因为我们讨论了只匹配正确的答案,这意味着我的答案不是很好。要理解要做什么并不总是容易的
https://regex101.com/r/J62XZq/7
在这里,我们寻找一个域后面没有
http:
或https:
的URL诀窍是在开头添加
\b
,以避免与URL内的URL匹配,并在域后使用负前瞻使用
(?!https?:)
完成了负前瞻(我没有添加双斜杠,因为我认为它已经足够了)带有
/next
的最后一部分可能不是必需的。这取决于您是否希望将URL与内部URL具体匹配使用:
它会找到正确的url,并从您的示例中拒绝其他url
Demo & explanation
输出:
相关问题 更多 >
编程相关推荐