开发者

Regular Expression for multiple zip codes with wild cards

I have to validate a comma seperated list of zip code such as "95123, 95%, 95222, 95%-8__, 95333"

i have the regular expression ^\d{5}(-?\d{4})?(,\d{5}(-开发者_运维技巧?\d{4})?)*$ that works to get valid zips without underscores and percentage characters. how can i modify the regular expression to work on the example string?


Why not split the string up on commas, then use the regular expression on each zip code?


As long as you're not too worried about validation:

(?:^|,\s*)([0-9\x25_]{1,5}(?:-[0-9\x25_]{1,4})?)

That should do what you need, just perform a match-all to get each entry.

Note: I don't do any checks to make sure they have enough numbers (like invalidate 123_ or %2_-__23 for instance if _ just means "1 character")

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜