RegEx for pin number

徘徊边缘 提交于 2021-02-10 14:48:42

问题


Im looking to create a RegEx for java to validate a Pin Number that needs to be at least exactly 6 character long and not all numbers can be equal.

I got /\d{6}/ but I I'm having a little trouble finding out how to make sure all 6 numbers should be different, so 000000 is invalid, while 000001 should be fine.


回答1:


You can use this regex with a negative lookahead assertion:

^(\d)(?!\1+$)\d{5}$

RegEx Demo

RegEx Details:

  • ^: Start
  • (\d): Match and capture first digit in group #1
  • (?!\1+$): Negative lookahead to assert we don't have repetitions of same digit till end
  • \d{5}: Match remaining 5 digits
  • $: End


来源:https://stackoverflow.com/questions/46435883/regex-for-pin-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!