• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex matching/compiling question

 
David Kaplowitz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is another good tutorial in the Java Tutorials.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic