How to match URIs in text?

后端 未结 7 667
轮回少年
轮回少年 2020-12-10 20:51

How would one go about spotting URIs in a block of text?

The idea is to turn such runs of texts into links. This is pretty simple to do if one only considered the ht

7条回答
  •  半阙折子戏
    2020-12-10 21:34

    That is not easy to do, if you want to also match "something.tld", because normal text will have many instances of that pattern, but if you want to match only URIs that begin with a scheme, you can try this regular expression (sorry, I don't know how to plug it in C#)

    (http|https|ftp|mailto|tel):\S+[/a-zA-Z0-9]
    

    You can add more schemes there, and it will match the scheme until the next whitespace character, taking into account that the last character is not invalid (for example as in the very usual string "http://www.example.com.")

提交回复
热议问题