jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Regex matching/compiling question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regex matching/compiling question" Watch "Regex matching/compiling question" New topic

Regex matching/compiling question

David Kaplowitz

Joined: Feb 08, 2005
Posts: 6

Basically what I want to do is to take a string, and see if there's a single non-letter character return a boolean value.

But I'm only getting a false when all chars are non-letter. Should I be iterating the String in a loop and doing a comparison of the chars, or is there a way to do it with the regexp and I'm just not seeing how to do it?

Thanks for any hints,

Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

To match a character that is not in a set of characters, you can use the ^ symbol (a circumflex accent) to mean 'not'. So [^A-Za-z] means find a character that is not an A-Z. You could also do [^A-Z] and set the case insensitive flag:

Pattern p = Pattern.compile("[^A-Z]", Pattern.CASE_INSENSITIVE);

That should work for what you want to do. Keep in mind this will only work on basic English letters. Letters from other languages, including accented letters will match the above pattern and be seen as a non-letter. Accented Characters are seen in English some times such as the word R�sum�.

p.s. www.regular-expressions.info has a great regex tutorial that might help you.
[ December 06, 2008: Message edited by: Mark Vedder ]
David Kaplowitz

Joined: Feb 08, 2005
Posts: 6
Thanks a lot, Mark. That info helped get me on track with the pattern match. I'll check out that tutorial you linked to.


Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46344
There is another good tutorial in the Java Tutorials.
I agree. Here's the link: http://aspose.com/file-tools
subject: Regex matching/compiling question
It's not a secret anymore!