Reputation: 427
I currently have a regular expression to validate a field within my application which looks like this:
^(?:(?:\w|[-])+\.(?:(?:(?:\w|[-])+|\.))*(?:\/(?:\w|[-])*)*|\w*)$
Unfortunately some aspects of this does not work.
aaa - Passes - Correct
aaa.aaa - Passes - Correct
aaa.aaa-aaa - Passes - Correct
aaa-aaa - Fails - Incorrect
How am I able to change my regular expression in order to make the last scenario pass?
Upvotes: 0
Views: 48
Reputation: 156978
The first \.
causes your last expression to fail. Since there are more groups, the first part of your expression has to match.
If you make the dot optional, your expression works.
Not sure, but maybe you can simplify the expression like this:
[A-Za-z]+([\-\.][A-Za-z]+)*
Upvotes: 1