need help with regex helicon rule - c#

I need some help with the regex as i am writing a new rule in the helicon.
the sample url will have file name and a query string parameter i want to match on both
www.testwebsite.com/hello.aspx?filename=/test.asp&employeeid=2100&age=20
in the above url i want to check if it is hello.aspx and has query string filename=/test.asp
filename can be anywhere in the querystring.
i want to break the above url into some other page
mynewpage.aspx $2$3 etc///
i wrote the following url but its not working , it matching pattern for all like sample1.aspx or any file name
(.*)(\/hello.aspx\?+)(.*)(filename=\/test\.asp)(.*)
any help will be appreciated

What you need are non capturing groups:
(?:.*)(\/hello.aspx\?+)(?:.*)(filename=\/test\.asp)(?:.*)
["www.testwebsite.com/hello.aspx?filename=/test.asp&employeeid=2100&age=20", "/hello.aspx?", "filename=/test.asp"]
(?:.*)(\/hello.aspx\?+)(?:.*)(filename=\/test\.asp)(.*)
["www.testwebsite.com/hello.aspx?filename=/test.asp&employeeid=2100&age=20", "/hello.aspx?", "filename=/test.asp", "&employeeid=2100&age=20"]
If you want to get all the parameters separately from the query string you can do it like this:
string queryString = (new Uri("...")).Query;
NameValueCollection parameters = HttpUtility.ParseQueryString(queryString);
parameters.Get("filename");
parameters.Get("employeeid");
parameters.Get("age");

Related

C# Regex URL Port username & password

I have a URL and need to extract the port, username and password from it and put them into an array. It looks like following.
http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts
Can I use some other method without replaces or substring?
One of the ways in C#
Get the query parameter
var parsedQuery = HttpUtility.ParseQueryString("http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts");
Then, below will give the username
parsedQuery["username"]
For Password:
parsedQuery["password"]
For port you can use URI :
Uri uri = new Uri("http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts");
Get the port by
uri.Port
Create an array or use whatever you require to club.
I don't know C#, but here's one that works for Python. It's pretty straightforward so you should be able to convert.
:(?P<port>[0-9]+).*username=(?P<username>[a-zA-Z0-9]+).*password=(?P<password>[a-zA-Z0-9]+)
The (?P<foo>bar) syntax is a named capture group that will put a variable matching the pattern 'bar' into a variable called 'foo' when you extract them.
Here is another possible solution with pure C# regex:
var url = "http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts";
var urlRegex = new Regex(#"(?<=(http(s)?://)?\w+(\.\w+)*:)\d+(?=/.*)?");
var usernameRegex = new Regex(#"(?<=(\?|&)username=).*?(?=&|$)", RegexOptions.IgnoreCase);
var passwordRegex = new Regex(#"(?<=(\?|&)password=).*?(?=&|$)", RegexOptions.IgnoreCase);
Console.WriteLine(urlRegex.Match(url));
Console.WriteLine(usernameRegex.Match(url));
Console.WriteLine(passwordRegex.Match(url));
If there are any parts that don't change, e.g. if it's always the same url you could just replace it like this
string str = "http://myproject.ddns.net:8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts"
str.Replace("http://myproject.ddns.net","");
This would leave you ":8080/get.php?username=9zu7T54rt6&password=1Tbliu49iH&type=m3u_plus&output=ts"
There is nothing stopping you repeating the process with another section.
As for regex you could use Regex.Match https://msdn.microsoft.com/en-us/library/twcw2f1c(v=vs.110).aspx to get the parts you want.
You could use ":\d{4}/" to get the port - you'd have to strip the leading ":" and trailing "/" though; this "username=\w*\&" to get the username - you'd have to strip the leading "username=" and trailing "&" though; and for the password you could use "password=\w*\&" - you'd have to strip the leading "password=" and trailing "&" though.
If you'd like to experiment with regex this site https://regex101.com/ is pretty good.

Query String issue when it contains Arabic text

I am trying to get query string from url With this code:
this.site_query = Request.Url.Query;
When I have get url:
http://localhost:1751/ar/search?q=سيارة
It gives me blow output in code:
http://localhost:1751/ar/Search?q=%D8%B3%D9%8A%D8%A7%D8%B1%D8%A9&Location=%D8%A3%D8%A8%D9%87%D8%A7,Abha
But I need Arabic text that I send in query string. When query string contains text in English then in c# it is correct.
There is nothing wrong with the second URL you have shown in your answer, it's just being URL encoded due to the limitations of what characters are allowed in URLs.
If you wish to get parts of the query string in code, you can use code like this:
var query = Request.QueryString["q"];
Additionally, if you are building your URLs in code, you should always URL encode and values that may contain non standard characters:
var urlEncodedValue = HttpUtility.UrlEncode(someValue);
As others said already: it's an encoded URL. You can decode with
var decodedUrl = HttpUtility.UrlDecode(url);
or
var decodedUrl = Uri.UnescapeDataString(url);
Is that what you need? If not, show us your expected output.
For this use
string name = HttpUtility.UrlEncode(Encrypt(txtName.Text.Trim()));
string technology = HttpUtility.UrlEncode(Encrypt(ddlTechnology.SelectedItem.Value));
for encoding url.

Replace Requested URL Section

I have a website that includes a language specifier in the URL (ex. http://example.org/English/rest/of/url.aspx)
Using a Regex, I can parse out the language of the URL:
Match match = Regex.Match(HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath, "^~/(?<language>[^/]+)/");
I can then check the parsed out language and determine if the language is what I want it to be:
match.Groups["language"].Value
I'm now looking for a simpler way than brutish string manipulation to replace only that language with a new language if needed.
So the URL above would be changed to http://example.org/German/rest/of/url.aspx
My initial thought was a simple search/replace however that won't work as the page name or other URL fragments may have the language name in them. I'm only concerned with the very first fragment after the root URL.
After changing the URL I would then redirect the user and be done with it.
Or you can use Regex.Replace:
string requestUrl = "~/English/rest/of/url.aspx";
string targetLanguage = "German";
Match match = Regex.Match(requestUrl, "^~/(?<language>[^/]+)/");
if (match.Groups["language"].Value != targetLanguage)
Response.Redirect(Regex.Replace(requestUrl, "^~/[^/]+/", string.Format("~/{0}/", targetLanguage)));
you can create a Uri object then use PathAndQuery to extract the then path split '/' and replace the first instance then construct your uri again

Extract Url from Javascript in

I am new in Regex can you please help in writing for regex in C# to extract url from text below?
Example 1
x+=1;
top.location.href = "http://www.keenthemes.com/preview/index.php?theme=metronic";
Example 2
alert("are you sure");
top.location.href = 'http://www.keenthemes.com/preview/index.php?theme=metronic';
If the URL always starts with http://, this one should do it:
["'](http.*)["']
The URL is stored in the second group (Groups[1].Value) of the Match object
(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])?
This will work for any kind of url. For more info please look at http://regexlib.com/Search.aspx?k=URL&AspxAutoDetectCookieSupport=1

C# Replace URL Regex

I am trying to pull a URL out of a string and use it later to create a Hyperlink. I would like to be able to do the following:
- determine if the input string contains a URL
- remove the URL from the input string
- store the extracted URL in a variable for later use
Can anyone help me with this?
Here is a great solution for recognizing URLs in popular formats such as:
www.google.com
http://www.google.com
mailto:somebody#google.com
somebody#google.com
www.url-with-querystring.com/?url=has-querystring
The regular expression used is:
/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+#)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+#)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%#.\w_]*)#?(?:[\w]*))?)/
However, I would recommend you go to http://blog.mattheworiordan.com/post/13174566389/url-regular-expression-for-links-with-or-without-the to see the working example.
Replace input with your input
string input = string.Empty;
var matches = Regex.Matches(input,
#"/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+#)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+#)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%#.\w_]*)#?(?:[.\!\/\\w]*))?)/");
List<string> urlList = (matches.Cast<object>().Select(match => match.ToString())).ToList();

Categories