This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Validate ID through RegEx Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Validate ID through RegEx" Watch "Validate ID through RegEx" New topic
Author

Validate ID through RegEx

André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234
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

Joined: Mar 13, 2009
Posts: 492

What have you tried so far?

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
André Asantos
Ranch Hand

Joined: Nov 23, 2009
Posts: 234


how could I print if the string teste is valid or not based on RegEx ready?
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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

Joined: Aug 08, 2009
Posts: 47
I think you will have to test the second condition manually.Using some String manipulation.


[Bsc in Computer Science and Engineering] [SCJP 80%][BCS certificate level]
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Don't get me started about those stupid light bulbs.
 
subject: Validate ID through RegEx
 
Similar Threads
identify data type in string?
treating variable as regex
This One is about regex !
how can i read from properties every thing start with foo.* ?
Regex