aspose file tools*
The moose likes Java in General and the fly likes Weird behavior with regular expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Weird behavior with regular expressions" Watch "Weird behavior with regular expressions" New topic
Author

Weird behavior with regular expressions

Dinkar Chaturvedi
Ranch Hand

Joined: Mar 27, 2007
Posts: 42

The following code works fine



The following doesn't.


Any idea why?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4388
    
    8

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

Joined: Mar 27, 2007
Posts: 42

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

Joined: Aug 16, 2005
Posts: 14149
    
  18

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?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Dinkar Chaturvedi
Ranch Hand

Joined: Mar 27, 2007
Posts: 42

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!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Weird behavior with regular expressions