Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
In my "ServiceEditModel" class i have a property Url with typeof Uri. For validation I search a Regex that check if the Url, which is filled up on my "Edit" page, is valid.
The Regex should check
Is there a http:// or https://
That the body only contains alphabetic characters and numbers
And the ending is like for example .com, .net, .ch
It should be possible, that there is another parameter behind the ending like for example https://stackoverflow.com/questions
My Code where the Regex comes in look like this:
[Required(ErrorMessageResourceType = typeof(Resources.ApplicationTemplate), ErrorMessageResourceName = "UrlRequired")]
[RegularExpression("REGEX COMES HERE", ErrorMessageResourceType = typeof(Resources.ApplicationTemplate), ErrorMessageResourceName = "InvalidUrl")]
public Uri Url { get; set; }
I already looked for Regex but can't find the right one because this is actually my first experiance with Regex.
Thanks for Help!
EDIT
I updated my regex so that it also allows url's with a "-" character such as http://www.comsoft-direct.ch/
Updated regex: ^(http|https):\/\/([\w\d + (\-)+?]+\.)+[\w]+(\/.*)?$
This Regex should check simple scenarios according to your constraints. You can easily play with it and improve it (which I strongly recommend, firstly because it's very simple at this state and secondly because you are a Regex beginner :)).
^(http|https):\/\/[\w\d]+\.[\w]+(\/[\w\d]+)$
Check it on Regex 101
Basic explanation:
(http|https):\/\/
Should start with http or https, followed by ://
[\w\d]+
Followed by N letters and/or digits
\.[\w]+
Followed by a dot and a set of letters. e.g.: .com, .net and such (note that you must change to \.[\d\w]+ to allow digits also)
(\/[\w\d]+)
Followed, optionally, by a / and a set of letters and/or digits (e.g.: /questions)
NOTE: If you want a full-generic url validator, you must then google for that.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have this regex example. I'm new to regex.
^[a-z0-9](\.?[a-z0-9_-]){0,}#[a-z0-9-]+\.([a-z]{1,6}\.)?[a-z]{2,6}$
I'm thinking where is the top level domain here, is it starting on the right or on the left? And what is it?
The regex you've provided won't allow Numbers in top level domains.
[a-z]{2,6}$ is the part checking the top level domain. It'll only allow lower case characters, minimal 2 characters max 6 characters.
EDIT: Let's deconstrunct your your regex for clearity.
^[a-z0-9](\.?[a-z0-9_-]){0,}#[a-z0-9-]+\.([a-z]{1,6}\.)?[a-z]{2,6}$
the ^[a-z0-9](\.?[a-z0-9_-]){0,} part checks from the beginning of the string if there are only allowed characters that are a-zand 0-9 followed by an optional . followed by a-zand 0-9 aswell as _ and - zero to infinite times.
#[a-z0-9-]+\.([a-z]{1,6}\.)? checks for an # after the first part aswell as validity of the domain, a-z and 0-9 including - followed by a . with an optional a-z2 to 6 times, up to one time i.E. google.com would be acceptable at this point but the .com part is optional.
[a-z]{2,6}$ checks for a-z 2 to 6 times and the end of the string indicated by $. meaning this is the part checking for the top level domain.
your regex would accept: blah#google.com aswell as blah#google.com.com
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
What would be a regex for matching "[d-n]", where n is any number?
i.e.
Test_4_[d-123] - returns ideally only 123
or, if I can return [d-123] I could make some string formatting.
(?<=\[d-)\d+(?=\]) will return 123 from Test_4_[d-123].
You give very little information as to how you want stuff matched.
A very simple solution could be:
\[d-(\d+)\]
From left to right:
\[ will match the literal character [
d- will match just d-
(\d+) will match any digit one or more times. It is in parenthesis, which makes it a capture/match group. This should mean that if you are using a regex tool/library, you should be able to retrieve the "first match group" and you should retrieve just 123.
\] will match the literal character ]
I can only suggest using a website like https://regex101.com/ which can help in creating regular expressions.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to use Regex to change my URL from this:
http://localhost:51577/Item/92MM+BLACK+CASE+FAN+W%2f+3+PIN+CONNECTOR+-+Cool+%26+Quiet/222069843383
Into a URL that would look like this:
http://localhost:51577/Item/92MM-BLACK-CASE-FAN-W-2f-3-PIN-CONNECTOR-Cool-26-Quiet/222069843383
Any %, + or +-+ sign would be replaced with - sign using regex. I think regex is the best solution for this, but I'm not so familiar with writing regex expressions... Can someone help me out with this?
Edit: Guys I have an even better idea... I have the Title name in controller in following format:
92MM BLACK CASE FAN W/ 3 PIN CONNECTOR - Cool & Quiet
How could I write an regex to replace white spaces and remove any extra white space (if there are any) in the string array...
Edit 2: Basically replacing any special character with a - sign... Any ideas?
https://msdn.microsoft.com/en-us/library/e7f5w83z(v=vs.110).aspx
Regex.Replace(YOUR_STRING, "[^0-9a-zA-Z]+", "-");
Try that
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to parse the email address from a mailto tag. I am looking for a way to do this via RegEx in C#.
Example input:
<mailto:abc#xyz.com>
Example output:
abc#xyz.com
In general, it's a very bad idea to use regular expressions for HTML parsing. Instead, take a look at the Html Agility Pack. For the specific input you provided, you may use:
(?<=\<mailto:).*(?=\>)
Here's a code sample:
var emailTag = "<mailto:abc#xyz.com>";
var emailValue = Regex.Match(emailTag, #"(?<=\<mailto:).*(?=\>)").Value;
Console.WriteLine(emailValue);
A simple Regex to strip anything in a mailto tag would be
<mailto:(.*?)>
You could use:
[\w\d]+\#[\w\d]+\.com
[\w\d] <----This matches any letter or character. \w matches any letter. \d matches anynumber.
+ <----One or more of previous item, in this case [\w\d]+ one or more letters or numbers
\# <----Simply matches the # symbol but it needs to be escaped with a \ as it is a special character
[\w\d]+ <----Same again
\. <---- Same concept as the # as . is a special character so it needs to be escaped
In your example:
[\w\d]+=abc
\#=#
[\w\d]+=xyz
\.=.
com=com
If your wanting to match special characters as well as letters and digits then just replace [\w\d]+ with [\S]+ (make sure s is capital).
[\S]+ <---Matches anything that is not a space.
You will have to do variations to include .co.uk and .org etc.
http://www.regular-expressions.info/reference.html <----This is very useful!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
When users create an account on my site I want to make server validation for emails to not accept every input.
I will send a confirmation, in a way to do a handshake validation.
I am looking for something simple, not the best, but not too simple that doesn't validate anything. I don't know where limitation must be, since any regular expression will not do the correct validation because is not possible to do it with regular expressions.
I'm trying to limit the sintax and visual complexity inherent to regular expressions, because in this case any will be correct.
What regexp can I use to do that?
It's possible to write a regular expression that only accept email addresses that follow the standards. However, there are some email addresses out there that doesn't strictly follow the standards, but still work.
Here are some simple regular expressions for basic validation:
Contains a # character:
#
Contains # and a period somewhere after it:
#.*?\.
Has at least one character before the #, before the period and after it:
.+#.+\..+
Has only one #, at least one character before the #, before the period and after it:
^[^#]+#[^#]+\.[^#]+$
User AmoebaMan17 suggests this modification to eliminate whitespace:
^[^#\s]+#[^#\s]+\.[^#\s]+$
And for accepting only one period [external edit: not recommended, does not match valid email adresses]:
^[^#\s]+#[^#\s\.]+\.[^#\.\s]+$
^\S+#\S+$
^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$
Only 1 #
Several domains and subdomains
I think this little tweak to the expression by AmoebaMan17 should stop the address from starting/ending with a dot and also stop multiple dots next to each other. Trying not to make it complex again whilst eliminating a common issue.
(?!.*\.\.)(^[^\.][^#\s]+#[^#\s]+\.[^#\s\.]+$)
It appears to be working (but I am no RegEx-pert). Fixes my issue with users copy&pasting email addresses from the end of sentences that terminate with a period.
i.e: Here's my new email address tabby#coolforcats.com.
Take your pick.
Here's the one that complies with RFC 2822 Section 3.4.1 ...
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
Just in case you are curious. :)