File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validate ID through RegEx

 
André Asantos
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried so far?

Hunter
 
André Asantos
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • 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 Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
lakmal padmakumara
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you will have to test the second condition manually.Using some String manipulation.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • 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.


 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic