Reputation: 11413
I have the following patterns in a URL.
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:
What am I doing wrong here?
Upvotes: 0
Views: 348
Reputation: 1094
Problems observed with your regex
\.
\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}$
Upvotes: 2
Reputation: 23
[a-zA-Z]\w*(.[-\w]+){1,2}$ works well...Check it out at http://regexr.com?3020g
Upvotes: 0
Reputation: 57996
Use this expression: \w+\S*?\.\w+\S*
I read your definition as:
This ran successfully using .NET RegexOptions.ECMAScript
and RegexOptions.Multiline
Upvotes: 0