如何分别获取url和其中包含的产品代码
how to get the url and the product code contained in it separately
我有一个邮件程序,我在其中发送一个 html 页面作为邮件正文。 html 页面有一些 link。我在这里做的是识别那些 links 并将那些 links 替换为 link 到我的页面并将 link 作为查询字符串中的参数连同我在文本框中输入的参数 'username'。这是代码-
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\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*([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}";
}));
里面有一个产品代码url。我想要的是产品代码以及 link。 link 可以是- http://example.in/next/pr-01.html
pr-01 是产品代码。产品代码采用这种格式 - pr-01,pr-02....
我是 .net 的新手,之前没有使用过正则表达式,所以我不知道如何获取产品代码并分别完成 link 并将它们传递到查询字符串中,如上所示
试试这个,
Regex myRegex = new Regex(@"pr-.*([\d])");
尽管可以选择使用正则表达式,但也可以不使用 (这甚至可能提高性能).
下面最后一段取自 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\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*([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 页面作为邮件正文。 html 页面有一些 link。我在这里做的是识别那些 links 并将那些 links 替换为 link 到我的页面并将 link 作为查询字符串中的参数连同我在文本框中输入的参数 'username'。这是代码-
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\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*([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}";
}));
里面有一个产品代码url。我想要的是产品代码以及 link。 link 可以是- http://example.in/next/pr-01.html pr-01 是产品代码。产品代码采用这种格式 - pr-01,pr-02.... 我是 .net 的新手,之前没有使用过正则表达式,所以我不知道如何获取产品代码并分别完成 link 并将它们传递到查询字符串中,如上所示
试试这个,
Regex myRegex = new Regex(@"pr-.*([\d])");
尽管可以选择使用正则表达式,但也可以不使用 (这甚至可能提高性能).
下面最后一段取自 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\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*([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.
}));