aspose file tools
The moose likes Java in General and the fly likes Regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Regular expression" Watch "Regular expression" New topic
Author

Regular expression

Komlavi Ekouevi
Greenhorn

Joined: Sep 14, 2011
Posts: 3
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
Bartender

Joined: Oct 14, 2005
Posts: 16482
    
    2

What makes you think it's not working?

(Hint: read our FAQ entry named TellTheDetails -- that's a link, follow it.)
Komlavi Ekouevi
Greenhorn

Joined: Sep 14, 2011
Posts: 3
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

Joined: Sep 28, 2004
Posts: 16681
    
  19

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: jrebel
 
subject: Regular expression
 
Similar Threads
Java Grammer/ Reg Ex For method identification
how to check non english string
CSV file in java
String parsing algorithm
How to make a regular expression that the order doesn't matter