I have a mailer in which i am sending a html page as the mail body. The html page has some links. What i am doing here is identifying those links and replacing those links with a link to my page and sending the link as a parameter in query string along with a parameter 'username' which i enter in a textbox. Here is the code-
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}";
}));
There is a product code in that url. What i want is the product code along with the link. The link can be- http://example.in/next/pr-01.html
pr-01 is the product code. The product code is in this format- pr-01,pr-02....
I am new to .net and haven't used regex earlier so i have no idea how to get the product code and complete link separately and pass them in query string as shown above
Try this,
Regex myRegex = new Regex(@"pr-.*([\d])");
Although using a regular expression is an option, it can also be done without (which might even be better for performance).
Here below the last segment is taken from the url (pr-01.html
),
followed by taking only the part of it before the file extension (.html
) starting with a .
,
which is the productcode 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
EDIT
The above parsing routine can be combined with your code as shown here below.
The last line shows how to include the productcode in the querystring (you might have to use an other querystring parameter name).
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.
}));