• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Check if a number contains all same digits using regular expression

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a requirement to validate a field. The field should be numeric, should not contain all the same digits (like 555555, 111111) and the length is 6 chars. I need a regular expression to check this. I used [1-9] to check if it's numeric. Now I need to know how can I check if it contains all same digits.

Thanks
Deepa
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepa Mj wrote:I used [1-9] to check if it's numeric. Now I need to know how can I check if it contains all same digits.



The regex to check for six of the same digits is "([1-9])\\1{5}". BTW, why only [1-9]? Why is digit zero not acceptable?

Henry
 
Deepa Jaine
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry.

BTW, why only [1-9]? Why is digit zero not acceptable?



Digit '0' is also acceptable. I missed it by mistake.

I have used the following for the validation


Now I would like to use a single regex instead of the above 2. Can you please provide that?

Thanks
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, can you take care of you screen name first? See this FAQ...

http://www.javaranch.com/name.jsp



Now I would like to use a single regex instead of the above 2. Can you please provide that?



I was hoping that when I showed you the solution, that you would be so wowed with regexes, that you would go out and figure out how it worked. And in doing so, learn other cool features of regexes. Cool features that does exactly what you want here.

If you really want to merge the two regexes. Take a look at the "zero width positive/negative lookahead" features.

Henry
 
Rancher
Posts: 4686
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepa Mj wrote:I need a regular expression to check this.


Why do you think you need a regex for this? Perhaps if you knew and loved regex, it would make sense. But a trivial for() loop with a test of each character would work, in all chances, be no slower, and would be a lot easier for someone else to undersand and maintain.
 
Whatever. Here's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic