Extract Url from Javascript in - c#

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

Related

Regular expressions redirection

I want to set redirection from
www.somesite.com/products/dynamicstring/randomtext1/randomtext2
to www.somesite.com/products/dynamicstring
Is it possible to do that through Regex ?
It means if my incming url is
www.somesite.com/products/myproducts/test1/test2 it should redirect to www.somesite.com/products/myproducts/
just briefing more about this :
#TomLord i am using HttpContext.Current.Response.RedirectPermanent(matchingDefinition.To) i have all the redirects "From" and "To" in a class object, in the form of REGEX expressions.Example in From "/product/*" and To "/products" , i am reading these object and trying to redirect them, but i am not able to redirect something like /products/dynamicstring/randomtext1/ to /products/dynamicstring where dynamic string is random string , i dont find any regular expression which can be use to do this. For example /products/samples/randomtext1 should redirect to /products/samples/
Redirection cannot be done with regex alone. Google a bit what is a regular expression in reality. The short answer is: it's string-like expression that describes search pattern. So it can't redirect, not even replace a substring with substring or do anything else then match and capture parts of the matched string.
That being said, regex can help us do what you wanna. I am gonna assume you can use Javascript, cause I can't put a solution in every language. I am also gonna assume you will try to go over the code not copy paste and press enter. If you only need that hire a programmer. If you use another language, principle should be the same:
obtain URL
define regex
use capture group to extract the part of your URL that you need
construct a new URL
redirect to it
While matching the URLs in general is a fair bit more complex, like:
^(?:https?://)?(?:[\w]+\.)(?:\.?[\w]{2,})+$
As long as you are sure you will only be getting URLs and in the format you wanna, we will do it far simpler.
Basically, let's say you have:
some text with 2 dots that ends in com
then a /products/dynamicstring/
then text
then /
then text
As a regex that is:
/\w*.\w*.com\/products\/dynamicstring\/\w*\/\w*/g
Curde matching is done, but we still need to add a capture group we will use to extract part of the string we need:
/(\w*.\w*.com\/products\/)dynamicstring\/\w*\/\w*/g
Oke, now let's leverage this regex to do rest of the work:
Define regex:
var regex = /\w*.\w*.com\/products\/dynamicstring\/\w*\/\w*/g;
Get current URL. If you already have URL use it.
var currUrl = window.location.href;
Extract capture group from string:
var match = regex.exec(currUrl);
Use that to get a new URL from old one:
var redirectUrl = match[1] + myproducts/
Finally, we redirect with:
window.location.replace(redirectUrl);
I wrote all this straight from my head so I recommend you go over each step, look how it works, read some documentation about functions used. You might find an error as well as learn a lot.

Using regex to split a formatted string to URL like StackOverFlow

I'm trying to write a parser that will create links found in posted text that are formatted like so:
[Site Description](http://www.stackoverflow.com)
to be rendered as a standard HTML link like this:
Site Description
So far what I have is the expression listed below and will work on the example above, but if will not work if the URL has anything after the ".com". Obviously there is no single regex expression that will find every URL but would like to be able to match as many as I can.
(\[)([A-Za-z0-9 -_]*)(\])(\()((http|https|ftp)\://[A-Za-z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?)(\))
Any help would be greatly appreciated. Thanks.
Darn. It seems #Jerry and #MikeH beat me to it. My answer is best, however, as the link tags are all uppercase ;)
Find what: \[([^]]+)\]\(([^)]+)\)
Replace with: $1
http://regex101.com/r/cY7lF0
Well, you could try negated classes so you don't have to worry about the parsing of the url itself?
\[([^]]+)\]\(([^)]+)\)
And replace with:
$1
regex101 demo
Or maybe use only the beginning parts to identify a url?
\[([^]]+)\]\(((?:https?|ftp)://[^)]+)\)
The replace is the same.

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();

How to get # value from query string?

Please help me to get query string value from the URL.
http://test.com/test.aspx#id=test
I tried to access with
Request.QueryString["id"]
Its getting null value.Please suggest how to access id from the url.
Thanks
I agree with everyone that the # should be a ?, but just FYI:
Note it isn't actually possible get the anchor off the URL, for example:
http://test.com/test.aspx#id=test
The problem is that # specified an anchor in the page, so the browser sees:
http://test.com/test.aspx
And then looks in the page for
<a id="test">Your anchor</a>
As this is client side you need to escape the # from the URL - you can't get it on the server because the browser's already stripped it off.
If you want the part after the # you have to copy it using Javascript before the request is sent to the server, and put the value in the querystring.
More info here c# get complete URL with "#"
A query string starts with a question mark ? not a hash #.
Try:
http://test.com/test.aspx?id=test
Using a hash, you're asking to jump to a named anchor within the document, not providing a query string
Your URL is not valid.
http://test.com/test.aspx#id=test
refers to a bookmark named id=test.
You should use
http://test.com/test.aspx?id=test
And then Request.QueryString["id"] will work.
If you would like to use it as hash tag you can use:
string value = Request.Url.ToString().Split('#')[1];
with this code, you will have your hash tag value.
Isnt it supposed to be?
http://test.com/test.aspx?id=test

need help with regex helicon rule

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");

Categories