C# code to linkify urls in a string

后端 未结 7 1403
Happy的楠姐
Happy的楠姐 2020-11-28 06:17

Does anyone have any good c# code (and regular expressions) that will parse a string and \"linkify\" any urls that may be in the string?

7条回答
  •  伪装坚强ぢ
    2020-11-28 06:44

    There is class:

    public class TextLink
    {
        #region Properties
    
        public const string BeginPattern = "((http|https)://)?(www.)?";
    
        public const string MiddlePattern = @"([a-z0-9\-]*\.)+[a-z]+(:[0-9]+)?";
    
        public const string EndPattern = @"(/\S*)?";
    
        public static string Pattern { get { return BeginPattern + MiddlePattern + EndPattern; } }
    
        public static string ExactPattern { get { return string.Format("^{0}$", Pattern); } }
    
        public string OriginalInput { get; private set; }
    
        public bool Valid { get; private set; }
    
        private bool _isHttps;
    
        private string _readyLink;
    
        #endregion
    
        #region Constructor
    
        public TextLink(string input)
        {
            this.OriginalInput = input;
    
            var text = Regex.Replace(input, @"(^\s)|(\s$)", "", RegexOptions.IgnoreCase);
    
            Valid = Regex.IsMatch(text, ExactPattern);
    
            if (Valid)
            {
                _isHttps = Regex.IsMatch(text, "^https:", RegexOptions.IgnoreCase);
                // clear begin:
                _readyLink = Regex.Replace(text, BeginPattern, "", RegexOptions.IgnoreCase);
                // HTTPS
                if (_isHttps)
                {
                    _readyLink = "https://www." + _readyLink;
                }
                // Default
                else
                {
                    _readyLink = "http://www." + _readyLink;
                }
            }
        }
    
        #endregion
    
        #region Methods
    
        public override string ToString()
        {
            return _readyLink;
        }
    
        #endregion
    }
    

    Use it in this method:

    public static string ReplaceUrls(string input)
    {
        var result = Regex.Replace(input.ToSafeString(), TextLink.Pattern, match =>
        {
            var textLink = new TextLink(match.Value);
            return textLink.Valid ?
                string.Format("{1}", textLink, textLink.OriginalInput) :
                textLink.OriginalInput;
        });
        return result;
    }
    

    Test cases:

    [TestMethod]
    public void RegexUtil_TextLink_Parsing()
    {
        Assert.IsTrue(new TextLink("smthing.com").Valid);
        Assert.IsTrue(new TextLink("www.smthing.com/").Valid);
        Assert.IsTrue(new TextLink("http://smthing.com").Valid);
        Assert.IsTrue(new TextLink("http://www.smthing.com").Valid);
        Assert.IsTrue(new TextLink("http://www.smthing.com/").Valid);
        Assert.IsTrue(new TextLink("http://www.smthing.com/publisher").Valid);
    
        // port
        Assert.IsTrue(new TextLink("http://www.smthing.com:80").Valid);
        Assert.IsTrue(new TextLink("http://www.smthing.com:80/").Valid);
        // https
        Assert.IsTrue(new TextLink("https://smthing.com").Valid);
    
        Assert.IsFalse(new TextLink("").Valid);
        Assert.IsFalse(new TextLink("smthing.com.").Valid);
        Assert.IsFalse(new TextLink("smthing.com-").Valid);
    }
    
    [TestMethod]
    public void RegexUtil_TextLink_ToString()
    {
        // default
        Assert.AreEqual("http://www.smthing.com", new TextLink("smthing.com").ToString());
        Assert.AreEqual("http://www.smthing.com", new TextLink("http://www.smthing.com").ToString());
        Assert.AreEqual("http://www.smthing.com/", new TextLink("smthing.com/").ToString());
    
        Assert.AreEqual("https://www.smthing.com", new TextLink("https://www.smthing.com").ToString());
    }
    

提交回复
热议问题