简体   繁体   English

Python 正则表达式再次匹配 - 匹配 url 第二部分

[英]Python regular expression again - match url part II

Ok.好的。 I get regexp from here (only WWW links, second version).我从这里得到正则表达式(只有 WWW 链接,第二个版本)。 Everything is fine, except one thing, it parse BBCode too.一切都很好,除了一件事,它也解析 BBCode。

Regexp正则表达式

(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))

What fails什么失败了

[img]http://example.foo/something.png[/img] [img]http://example.foo/something.png[/img]

When I apply regexp I get http://example.foo/something.png[/img and that's fail.当我应用正则表达式时,我得到 http://example.foo/something.png[/img 并且失败了。 :P Any regexp guru there? :P 那里有正则表达式大师吗?

This is a little rough, but try this:这有点粗糙,但试试这个:

$preg = "%(?:https?://|www\d{0,3}.)(?:[\/A-Za-z0-9-_.]+(?!(?:<|\[/([A-Za-z0-9])+?\1)))%";

I've tested it and it should work as expected if I understood your question correctly.我已经对其进行了测试,如果我正确理解了您的问题,它应该可以按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM