开发者

Finding links in raw text string

I'm in a situation where I have a string of raw text where I want to find all links (starting with Http://) and place <a href="thelink"> before the start of the link and then a </a> after the link. The problem is, that I don't know when the link ends. I.e.:

(http://www.mylink.com)

In the above example, I am able to find the start of the link: http:// but I don't know when the link ends, so it would take the ) aswell.

Can 开发者_如何学编程anyone shed some light on this? Should I be using some sort of a RegEx pattern?

Thanks a lot in advance.

All the best,

Bo


According to RegexBuddy use this as your regex

\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[A-Z0-9+&@#/%=~_|]

and this as your replace syntax

<a href="\0">\0</a>

The logic is to look for all valid URL characters and stop once you encounter an invalid one like a space or a right parenthesis, for example.

If you haven't tried out their tool, I highly recommend it. It's a great learning tool as it dissects regular expressions and converts them into plain English.

It also comes with a huge library of useful expression such as this one and it's quite cheap.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜