正则表达式URL - 用链接替换匹配

时间:2017-03-29 15:34:06

标签: c# regex

我尝试编写一个关于这些点的URL(带替换)的正则表达式:

  • http://www.test.com 是正确的 =&GT; <a href="http://www.test.com">www.test.com</a>
  • https://www.test.com 是正确的 =&GT; <a href="https://www.test.com">www.test.com</a>
  • http://test.com 是正确的 =&GT; <a href="http://test.com">test.com</a>
  • www.test.com 是正确的 =&GT; <a href="http://www.test.com">www.test.com</a>
  • test.com不正确

我做了什么:

// __url__ is the rest of the regex but NOT IMPORTANT in my problem
var regex = new Regex(@"((https?:\/\/)?(?<URL>www\." + __url__ + "))");
regex.Replace("www.test.com", "<a href=\"http://${URL}\">${URL}</a>");

这里有一些问题:

- Test 1 OK
- Test 2 KO
  I force http as protocol
- Test 3 KO
- Test 4 OK

1 个答案:

答案 0 :(得分:1)

您可以捕获协议或www.部分以强制要求(使用(?:(https?:\/\/)|(www\.)))。这样,您将避免匹配test.com。然后,您只需要在匹配评估器中使用动态替换逻辑:

var inputs = new[] {"http://www.test.com",  "https://www.test.com", "http://test.com", "www.test.com","test.com" };
foreach (var s in inputs)
{
    var res = Regex.Replace(s,@"(?:(https?:\/\/)|(www\.))(\S+)", m => 
        m.Groups[1].Success ? 
            string.Format("<a href=\"{0}{1}{2}\">{1}{2}</a>", m.Groups[1].Value,m.Groups[2].Value,m.Groups[3].Value) : 
            string.Format("<a href=\"http://{0}{1}\">{0}{1}</a>", m.Groups[2].Value,m.Groups[3].Value) 
        );
    Console.WriteLine("{0} => {1}", s, res);
}

请参阅C# online demo

输出:

http://www.test.com => <a href="http://www.test.com">www.test.com</a>
https://www.test.com => <a href="https://www.test.com">www.test.com</a>
http://test.com => <a href="http://test.com">test.com</a>
www.test.com => <a hr
相关问题