我目前正在尝试匹配Markdown文本中的所有纯文本链接。
Markdown文本示例:
Dude, look at this url http://www.google.com .. it's a great search engine
我希望它能够转换为。
Dude, look at this url <http://www.google.com> .. it's a great search engine
简而言之,处理普通文本中的
url
应该变成<url>
,但处理现有的<url>
不应该变成<<url>>
。此外,Markdown中的链接可以采用(url)
的形式,因此我们需要避免匹配普通的括号。在Java中,我用于匹配纯文本URL的工作正则表达式是:
"[^(\\<|\\(](https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|][^(\\>|\\)]"
,其中使用了[^(\\<|\\(]
和[^(\\>|\\)]
来避免匹配包含在尖括号或圆括号中的URL。但是问题在于,我也不想匹配这种类型的URL:
[1]: http://slashdot.org
所以,如果Markdown文本如下:
Dude, look at this url http://www.google.com .. it's a great search engine
[1]: http://slashdot.org
我希望只匹配http://www.google.com
,而不是http://slashdot.org
。
我想知道满足这个标准的模式是什么?