• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Validate ID through RegEx

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please how could I validade the ID using the following rules through RegEx:

* must have 11 numbers (only numbers are permitted);
* numbers like 111111111111 22222222222 333333333333 are false...
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you tried so far?

Hunter
 
André Asantos
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


how could I print if the string teste is valid or not based on RegEx ready?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have the code to do this already in your for-each loop. Just create a matcher object like you did in the loop and print the results of mat.matches()

Hunter
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you will have to test the second condition manually.Using some String manipulation.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Use a construct called a "negative lookahead".


The above regex pattern consist of four parts: ^ + (?!pattern) + \d{11} + $.

^ , \d{11} and $ are very basic constructs I expect everybody should know, so we skip them here.

(?!pattern) pattern is called a negative lookahead. This construct checks if a pattern (inside brackets) matches the input string,
and if yes - the regex engine reports failure of the whole pattern at this point...
- if no, the regex engine continues trying to match the rest of the pattern (after the negative lookahead).
Lookaheads has one important attribute - they do not consume characters from the input string,
so when this pattern matches, then the regex engine continues matching next part of the pattern starting from the same input string position.

Inside brackets there is a (\d)\1{10} pattern - here round brackets constructs a backreference.
Backreferences allow to reuse part of regex match in other plases of the pattern.
In the above pattern \1 is a reference to a pattern backreference number 1 (placed in the first () pair in the pattern)
and it is substituted with input characters matched with this pattern inside brackets.
The whole pattern (\d)\1{10} matches if the first character is a digit (\d) and the next character - \1 - repeated 10 times - {10}
is the same character as the first character matched by pattern \d inside brackets.

Summarizing - (\d)\1{10} checks if the input stream contains 11 same digits,
if yes - the surrounding pattern (?!pattern) makes the whole pattern to fail,
otherwise the regex engine continues matching next \d{11} pattern.

I am not sure if my explanation is clear enough, refer to this page for more details: http://www.regular-expressions.info/tutorial.html
This is the most excellent regex tutorial I have seen on the web.


 
Sheriff
Posts: 22817
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is another case where a simple loop combined with Character.isDigit and the first character to compare all other characters with* would be easier.

* If at least one character is different from the first character than the number should be valid, right?
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic