C#: RegEx for Url validation [duplicate] - c#

This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
I need a regEx to match general URLs
(3 answers)
Closed 9 years ago.
I need a Regex validating Url:
This should be valid
http://www.google.com
https://www.google.com
but not this:
google.com
www.google.com
I know this can be done with Uri.IsWellFormedUriString for example but I need a regex. Found a couple of similar topics but does not fit my case.Thanks!

Are you sure you want regex and not this - Uri.TryCreate?
Also have you gone through this post - What is the best regular expression to check if a string is a valid URL?

(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])?

try this
Regex urlchk = new Regex(#"((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,15})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?", RegexOptions.Singleline | RegexOptions.IgnoreCase);

Related

How to build a string regex pattern for number range from 0000-4095 [duplicate]

This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 3 years ago.
I want to build a regex to search number in a string (using c#) with range from 0000-4095. I use this string pattern:
string regex_pattern = (0?[0-3][0-9][0-9][0-9]|{4}{0}[0-9]{0}|{4}{0}{9}[0-5]);
But i can not get success.
Can you please show me some hints?
Thanks
You could use
^([0123][0-9][0-9][0-9]|40[0-8][0-9]|409[0-5])$
Regex Demo
Sample
sorry for the multiples groups:
([0-4]0(([0-9][0-5])|([0-8][0-9])))|([0-3][0-9]{3})
Sample

verify that string matches a regex pattern [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
Is there a way to verify with Regex that a string matches a patter like so:
1.001
1.002
15.001
Where the pattern is:
any number followed by
dot character
followed by three (3) numbers
Given that patters things like this would fail:
A.001
1.0001
1,001
...
Any help is appreciated.
This regex should work:
^\d+\.\d{3}$

regex expression to accept ENTER key in string [duplicate]

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 6 years ago.
Issue
I am having an issue creating a regex to accept any string and the ENTER key, at the moment i have this:
^$|^.+$
I have looked around and people have said to add \n but this does not work.
An example of the string is should allow is as follows:
Hello this is a test string
and i want this to be accepted
Try setting the s flag on the regex engine. This will ensure that the . metacharacter will match newlines.
Here's a link to a working example.
Also, as a sidenote, instead of ^$|^.+$ you can condense the whole expression to ^.*$ to achieve the same results with better performance.
In C#, you need the RegexOptions.Singleline option. See this SO post for more information.
Here is a quick example that really just matches the entire string, so it's not useful.
var regex = new Regex(#"^.*$",
RegexOptions.IgnoreCase | RegexOptions.Singleline);
In your future validation code, you need to replace .* with whatever your validation will be.

Regular Expression that matches any .aspx url [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
The text I was going through mentioned that following regular Expression matches any .aspx url:
#"?i:^.*\.aspx.*$"
I couldnot understand what ?i:^ did while matching. Pls. explain what part it matches in urls like http://localhost:2447/Out.aspx, https://msdn.microsoft.com/en-us/library/88c54tsw.aspx.
?i means ignore case. Your pattern also has some unneeded fillers. If you only want to check if the string contains .aspx, use this:
(?i)\.aspx
// Match:
http://localhost:2447/Out.aspx
http://localhost:2447/Out.AsPx/suburl
https://msdn.microsoft.com/en-us/library/88c54tsw.aspx

Check if a string in C# is a URL [duplicate]

This question already has answers here:
Regular expression for URL
(10 answers)
Closed 6 years ago.
This has been asked before here, but the answers are all PHP related.
Is there a similar and working solution using C#? Like a specific test class or routine?
I want to parse www.google.com or google.com or mywebsite.net etc... with or without prefixes.
Thanks
C# has Regex as well, but this seems simpler:
bool isUri = Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute);
(Answered at Regular expression for URL)
https://msdn.microsoft.com/en-us/library/system.web.webpages.validator.regex(v=vs.111).aspx
you use the above mentioned class or use the below regex and check Regex matches with your url string
Regex UrlMatch = new Regex(#"(?i)(http(s)?:\/\/)?(\w{2,25}\.)+\w{3}([a-z0-9\-?=$-_.+!*()]+)(?i)", RegexOptions.Singleline);
Regex UrlMatchOnlyHttps = new Regex(#"(?i)(http(s)?:\/\/)(\w{2,25}\.)+\w{3}([a-z0-9\-?=$-_.+!*()]+)(?i)", RegexOptions.Singleline);
you can also use the above regexpattern to validate the url
You can use this way:
bool result = Uri.TryCreate(uriName, UriKind.RelativeOrAbsolute, out uriResult)
&& uriResult.Scheme == Uri.UriSchemeHttp;

Categories