【问题标题】:how to get the url and the product code contained in it separately如何分别获取其中包含的url和产品代码
【发布时间】:2019-01-27 05:34:32
【问题描述】:

我有一个邮件程序,我在其中发送一个 html 页面作为邮件正文。 html页面有一些链接。我在这里所做的是识别这些链接并将这些链接替换为指向我的页面的链接,并将链接作为查询字符串中的参数以及我在文本框中输入的参数“用户名”发送。这是代码-

StreamReader reader = new StreamReader(Server.MapPath("~/one.html"));
                string readFile = reader.ReadToEnd();
                Regex regx = new Regex("(?<!src=\")http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*([a-zA-Z0-9\\?\\#\\=\\/]){1})?", RegexOptions.IgnoreCase);
                string output = regx.ToString();    
                 output = readFile;
                string username = Server.UrlEncode(this.txtUsername.Text);

                output = regx.Replace(output, new MatchEvaluator((match) =>
                  {   
                var url = Uri.EscapeDataString(match.Value.ToString());
                  return $"http://localhost:61187/two?sender={username}&link={url}";
                 }));

该网址中有一个产品代码。我想要的是产品代码以及链接。链接可以是-http://example.in/next/pr-01.html pr-01 是产品代码。产品代码采用这种格式 - pr-01,pr-02.... 我是 .net 新手,之前没有使用过正则表达式,所以我不知道如何分别获取产品代码和完整链接并将它们传递到查询字符串中,如上所示

【问题讨论】:

    标签: c# .net regex smtp query-string


    【解决方案1】:

    虽然使用正则表达式是一种选择,但也可以不使用 (这可能会更好地提高性能)
    下面最后一段取自 url (pr-01.html),
    然后只取文件扩展名(.html)之前的一部分,以.开头,
    这是产品代码pr-01

    String url = "http://example.in/next/pr-01.html";
    Uri uri = new Uri(url, UriKind.Absolute);
    String fileName = uri.Segments[uri.Segments.Length - 1]; // pr-01.html
    String productCode = fileName.Substring(0, fileName.IndexOf(".")); // pr-01
    

    编辑

    上述解析例程可以与您的代码结合使用,如下所示。
    最后一行显示了如何在查询字符串中包含产品代码(您可能必须使用其他查询字符串参数名称)

    StreamReader reader = new StreamReader(Server.MapPath("~/one.html"));
    string readFile = reader.ReadToEnd();
    Regex regx = new Regex("(?<!src=\")http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*([a-zA-Z0-9\\?\\#\\=\\/]){1})?", RegexOptions.IgnoreCase);
    string output = regx.ToString();    
    output = readFile;
    string username = Server.UrlEncode(this.txtUsername.Text);
    
    output = regx.Replace(output, new MatchEvaluator((match) =>
    {  
        Uri uri = new Uri(match.Value, UriKind.Absolute);
        String fileName = uri.Segments[uri.Segments.Length - 1]; // pr-01.html
        String productCode = Uri.EscapeDataString(fileName.Substring(0, fileName.IndexOf("."))); // pr-01
    
        var url = Uri.EscapeDataString(match.Value.ToString());
        return $"http://localhost:61187/two?sender={username}&link={url}&productcode={productCode}"; // << Include the productcode in the querystring.
    }));
    

    【讨论】:

    • 我必须使用正则表达式,因为我不知道 html 页面中的哪个链接,用户会点击。 “example.in/next/pr-01.html”——这只是一个例子。我想要使​​用正则表达式的解决方案
    • 我想要的是-在链接中有一个产品代码,即链接包含产品代码。所以我想同时获得链接和子部分(产品代码)。在此之后,我将用我的链接替换链接并将链接名称和产品名称作为参数发送到查询字符串
    • 您能检查一下这段代码是否可以处理您拥有的其他一些网址吗?只要最后一部分匹配模式:productcode + . 就会被解析; productcode前面的部分无关紧要。
    • 你能使用我上面使用的方法提供解决方案吗?(使用正则表达式)
    • 你想如何使用urlhttp://localhost:61187/two?sender={username}&amp;link={url}中的productcode?它是否必须作为单独的查询字符串参数附加?
    【解决方案2】:

    试试这个,

    Regex myRegex = new Regex(@"pr-.*([\d])");
    

    【讨论】:

    • 如何返回产品代码?此外,产品代码是链接(regx)的一部分
    • string str = "http://example.in/next/pr-0134534.html"; Regex myRegex = new Regex(@"pr-.*([\d])"); Match myMatch = myRegex.Match(str); string fileName = myMatch.Groups[0].Value; 在 FileName 中,您将获得您的产品代码。
    • output = regx.Replace(output, new MatchEvaluator((match) =&gt; { var url = Uri.EscapeDataString(match.Value.ToString()); Regex myRegex = new Regex(@"pr-.*([\d])"); Match myMatch = myRegex.Match(url); string fileName = myMatch.Groups[0].Value; return $"http://localhost:61187/two?sender={username}&amp;link={url}&amp;productCode={fileName}"; })); 请检查此代码
    • 如果产品代码类似于 abc01 或 trk-54 或 product-abc 怎么办?那我们怎么才能得到呢?我们拥有的唯一信息是产品代码将在链接中的最后一个 \ 之后和 .喜欢-xyz/shop/health/product-abc1.html?在这种情况下,什么是正则表达式模式来代替 this-@"pr-.*([\d])"
    • 正则表达式 myRegex = new Regex(@"[^\/]?.*\/(.*)?.*\/(.*)\/(.*)?.*\. ");匹配 myMatch = myRegex.Match(Uri.UnescapeDataString(url));字符串文件名 = myMatch.Groups[3].Value;试试这个正则表达式并检查
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多