Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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: 23811
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: 62170
193
  • 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.
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!