Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Weird behavior with regular expressions

 
Dinkar Chaturvedi
Ranch Hand
Posts: 42
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code works fine



The following doesn't.


Any idea why?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matcher.matches() tries to match the entire string to the pattern. Your pattern will match against any number of 'v's, but it won't match anything that isn't a 'v'.

Perhaps you intended Pattern.compile("v.*") ?
 
Dinkar Chaturvedi
Ranch Hand
Posts: 42
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm!! That is interesting.

What if I want to find if the pattern v* occurs in the string vyadav?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15484
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look in the API documentation of class Matcher - there are other methods besides matches() that do what you are looking for.

Here's a hint...:
Dinkar Chaturvedi wrote:What if I want to find if the pattern v* occurs in the string vyadav?
 
Dinkar Chaturvedi
Ranch Hand
Posts: 42
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!! I didnt realize that there are other methods as I am used to the functionality with find() method instead of looking for the entire string as a match.

Thanks anyway, this helped a big lot!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic