• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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

 
Ranch Hand
Posts: 73
  • 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?
 
author
Posts: 23830
140
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
 
Marshal
Posts: 64190
215
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!