Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java.util.regex.Pattern returning false?  RSS feed

 
Matt Taylor
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I would like to check for a string. If the string contains a special character, I would like to return true. But why in my code it returns false?



How can I make modifications so that this returns true?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your pattern is trying to match a single character that is not numbers or letters. And you are doing a whole string match (with the matches() method).... and since your input string is not a single character, there is no way that it will match.

Perhaps you wanted to find() the pattern, and not matches() the pattern?

Henry
 
Campbell Ritchie
Marshal
Posts: 55680
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a nice section about regular expressions in the Java™ Tutorials.
You need to think long and hard before writing a regular expression, as you have seen here.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!