【问题标题】:How to Use A MaskType to validate http:\\ links and to validate end of that link with .xml如何使用 MaskType 验证 http:\\ 链接并使用 .xml 验证该链接的结尾
【发布时间】:2013-12-28 09:15:43
【问题描述】:

我知道这个问题可能是副本或重复或类似于 URL 的验证。但差别不大:我想对以http:\\---- 开头并以.xml 结尾的链接进行验证。我通过以下链接使用了以下代码行:Regular Expression for URL validation

  string myString = textBox3.Text;
        Uri myUri;
        if (Uri.TryCreate(myString, UriKind.RelativeOrAbsolute, out myUri))
        {
            textbox1.Text = myUri.ToString();

            XmlReader reader = XmlReader.Create(myUri.ToString());

        }
        else
        {
            textbox2.Text = "NOT VALID";
        }
    }

但是通过在http:\\ 之后仅提取字符串来传递值 谁能回答我的问题:)

【问题讨论】:

    标签: c# .net xml wpf xmlhttprequest


    【解决方案1】:

    所以你的问题是你想验证一些以http:\\ 开头的文本(大概https:\\.xml 结尾并且是一个有效的Uri?

    尝试使用RegexUri.TryCreate之类的这种静态方法。

    /// <summary>
    /// the regex to match. Uses the Ingore case flag, compiled and single line
    /// </summary>
    readonly static Regex xmlUriRegex = new Regex("^http(s?)://(.*).xml$", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);
    
    /// <summary>
    /// Checks if the string input is a valid XML uri that starts with an http protocol, ends with .xml and is a valid URI
    /// </summary>
    /// <param name="xmlUri">the uri</param>
    /// <returns></returns>
    public static bool IsValidXMLUri(string xmlUri)
    {
        Uri u = null;
        return Uri.TryCreate(xmlUri, UriKind.Absolute, out u) && xmlUriRegex.IsMatch(xmlUri);
    }
    

    希望这会有所帮助。

    编辑:正则表达式更新。

    正则表达式^http(s?)://(.*).xml 确保协议必须是http://https://,并且必须在字符串中包含.xml。现在我更改了正则表达式以强制将.xml 文件扩展名放在字符串的末尾:^http(s?)://(.*).xml$

    【讨论】:

      猜你喜欢
      • 2014-09-08
      • 2019-09-04
      • 2011-02-02
      • 1970-01-01
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      相关资源
      最近更新 更多