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;
Related
This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
How to make a regex match case insensitive?
(1 answer)
Closed 5 years ago.
I am trying to do a find replace in a string of text. I am using Regex like this:
Regex regexText = new Regex("Test.Value");
strText = regexText.Replace(strText, value);
In this example I am trying to find the string "Test.Value" in a text string. However if this value appears in the string the replace does not happen.
If I remove the dots eg:
Regex regexText = new Regex("TEST");
strText = regexText.Replace(strText, value);
If I put the word "TEST" in the string, it replaces it just fine.
Is there a way to get this to work with strings with "."'s in?
You have to escape the dot:
Regex regexText = new Regex(#"Test\.Value");
As you wrote it, the regex is just looking for "Test", followed by any character except a line feed, followed by "Value".
On the top of that, if the text you are looking for is a little bit different, a case insensitive matching could help you out:
Regex regexText = new Regex(#"Test\.Value", RegexOptions.IgnoreCase);
Anyway, in this case I don't think a Regex is necessary. A simple string replace should do the job:
strText.Replace("Test.Value", value);
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.
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
I have the following text
"a|mother" "b|father"
I want to find via Regex, groups of text that starts with '"' and ends with '"' and separate with '|' without spaces. Meaning the results would be:
"a|mother"
"b|father"
I try to use other posts to solve my question but still with no luck how can I find the |? and how can I find my pattern without spaces?
Something like this:
String source = "\"a|mother\" \"b|father\"";
var result = Regex
.Matches(source, "\"[^\"]*[^ ]\\|[^ ][^\"]*\"")
.OfType<Match>();
Console.Write(String.Join(Environment.NewLine, result));
Output is
"a|mother"
"b|father"
This question already has answers here:
How does one parse XML files? [closed]
(12 answers)
Closed 9 years ago.
I have a xml file:
"<?xml version=\"1.0\" encoding=\"utf-8\"?><response list=\"true\"><count>2802</count><post><id>4210</id><from_id>2176594</from_id><to_id>-11423648</to_id><date>1365088358</date><text>dsadsad #ADMIN</text>...
I want to take a string between <from_id> and </from_id>.
I have a regex exprassion <from_id>(.*?)</from_id>, but it return string with tags.
Can you help me?
Waiting for response)
P.S.: Sorry for my poor english!
As others have already pointed out, you'd probably be on a safer and cleaner side by using an XML parser.
That said, you've already got a working regular expression. Just make sure to retrieve the capture group #1. That will get you just what is inside the first pair of parentheses.
If you're using C#, instead of calling toString() on the Match, look into its Groups property and get its first element:
string pattern = "<from_id>(.*?)</from_id>";
string input = "<?xml version=\"1.0\" encoding=\"utf-8\"?><response list=\"true\"><count>2802</count><post><id>4210</id><from_id>2176594</from_id><to_id>-11423648</to_id><date>1365088358</date><text>dsadsad #ADMIN</text>";
Match match = Regex.Match(input, pattern);
if (match.Success){
System.Console.WriteLine(match.Groups[1].Value);
}
See it working in this Ideone snippet.
If you wanted to get all matches of the pattern, you could use Regex.Matches() instead, and iterate over each Match in the MatchCollection in the same way.
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);