• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding Regex in java .  RSS feed

 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All ,

I am trying below code to match if there any characters other than 0 and 1 in the String, but seems i am missing something.



Both returns false ,first one should give me true and second as false. Kindly let me know if it is correct.

- Chetan
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37511
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost. Try "[^01]+". Which means one or more characters that aren't 0 or 1. The difference is that matches() is checking the whole string. So in your regex, it says "is there exactly one character that isn't 0 or 1". Nope, there are a lot of them. So it returns false.

Whereas adding the +, it checks all the characters until it finds one that doesn't match or hits the end of the string.
 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeanne ,

It works , thanks for clarification...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!