I have the following patterns in a URL.
- John.Smith
- John.Smith.1
- John.Al-Smith
- John.al-smith.1
- John.Smith.Al-Caboon
Where the first (.) is mandatory and with at least one character before and after the first (.), the rest of the stuff (the numbers, hyphen, and the second (.)) are optional.
I created the following Regex:^\w+.\w+-*\w*.?\d*\w*-*\w*
Though it successfully matched all the above patterns, it also matches some undesired patterns like:
- "login" (Without the mandatory first dot)
- "users/jo开发者_StackOverflow社区hn" (with an undesired / and also without the mandatory first dot)
- "1234" (Invalid, the pattern has to start by a character)
What am I doing wrong here?
Problems observed with your regex
- "." is a meta character in regex. It matches "anything". You should escape it to match the dot. Like this:
\.
\w
is a character class which includes small letters, caps, numbers and underscore. This explains why "1234" passed.
Try this
^[a-zA-Z]\w*(\.[-\w]+){1,2}$
maybe you should escape the dots
\.
Use this expression: \w+\S*?\.\w+\S*
I read your definition as:
- at least one character
- mandatory dot
- at least one character
This ran successfully using .NET RegexOptions.ECMAScript
and RegexOptions.Multiline
[a-zA-Z]\w*(.[-\w]+){1,2}$ works well...Check it out at http://regexr.com?3020g
And \w
means [A-Za-z0-9_]
. Which is why it matches "1234".
精彩评论