• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regular expression  RSS feed

 
Komlavi Ekouevi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help to set up a regular expression of 12 charaters. The first 7 characters must be alpha characters and the 5 remaining characters must be numeric. Below is my line of code
Pattern numberPattern = Pattern.compile( "[a-zA-Z]{7}[0-9]{5}" );
Can somebody fix that for me because it's not working... :-(
Thanks
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think it's not working?

(Hint: read our FAQ entry named TellTheDetails -- that's a link, follow it.)
 
Komlavi Ekouevi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is my code

String orderNumber = "abcdedg123453" ;
Pattern numberPattern = Pattern.compile( "[a-zA-Z]{7}[0-9]{5}" );
Matcher numberMatcher = numberPattern.matcher( orderNumber );
return numberMatcher.find( );

I passed 13 characters but it returned true. I would like this code to return false every time orderNumber is not 12 characters. Also the first 7 characters must be alpha characters and the five remaining must be digit.
Thanks
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Komlavi Ekouevi wrote:Below is my code

String orderNumber = "abcdedg123453" ;
Pattern numberPattern = Pattern.compile( "[a-zA-Z]{7}[0-9]{5}" );
Matcher numberMatcher = numberPattern.matcher( orderNumber );
return numberMatcher.find( );

I passed 13 characters but it returned true. I would like this code to return false every time orderNumber is not 12 characters. Also the first 7 characters must be alpha characters and the five remaining must be digit.
Thanks


Use the matches() method instead. Take a look at the JavaDoc (java.util.regex.Matcher) to see why.

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!