This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Regex matching/compiling question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Regex matching/compiling question" Watch "Regex matching/compiling question" New topic
Author

Regex matching/compiling question

David Kaplowitz
Greenhorn

Joined: Feb 08, 2005
Posts: 6
Hello,

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,

Dave
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
Greenhorn

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.

Best,

Dave
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32695
    
    4
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
 
Similar Threads
Regex validation
regex for nameFields: first & last names tested separately
jdk1.4 regex help
help requested with regular expressions
How to regexes with negations / exceptions