I need a regualr expression that accepts the following input
1 Day
4 Days
15 Days
1 Month
12 Months
1 Year
5 Years
The numbers may have any length. An开发者_开发技巧y ideas?
Try this,
^(1 (Day|Month|Year)|[1-9][0-9]* (Days|Months|Years))$
It will not accept what I assume is invalid input such as "5 Day" and "04 Months"
Explanation:
Either the input is "1 Day/Month/Year" or it is a digit 1-9 followed by any number of digits 0-9 followed by a space and one of the strings "Days" "Months" or "Years"
\d+\s+(Day|Month|Year)(s)?
- \d+ at least one digit
- \s+ at least one space character
- (Day|Month|Year) Day or Month or Year
- (s)? optionally pluralize the unit
精彩评论