一千萬個為什麽

搜索

如何解析字符串中的鏈接

I have a string like "Go to stack overflow http://stackoverflow.com" Now I want to parse link from this string using c#. I wants the output in the following format. "Go to stack overflow http://stackoverflow.com</>"

在C#中有可能嗎?

最佳答案

var input = @"Go to stack overflow http://stackoverflow.com";
var result = Regex.Replace(input, 
    @"((?\w+):\/\/(?[\[email protected]][\w.:@]+)\/?[\w\.?=%&=\[email protected]/$,]*)",
    @"Go to stack overflow $1</>");

輸出:

Go to stack overflow Go to stack overflow http://stackoverflow.com</>

轉載註明原文: 如何解析字符串中的鏈接