Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Using java.util.regex.Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Using java.util.regex.Pattern" Watch "Using java.util.regex.Pattern" New topic
Author

Using java.util.regex.Pattern

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Why this line

java.util.regex.Pattern.matches("input","input text");

return false?

I'm expecting true for a match. What's wrong with it?
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12952
    
    3

Well, the string "input text" simply doesn't match with the regular expression "input". Try this:

The regular expression "input.*" means: the letters "input" followed by zero or more arbitrary characters.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26496
    
  78


Note the different between matches and find. Matches returns false because the entire regular expression doesn't match. Find returns true because the regular expression is found within the string.

Pattern.matches() is shorthand for the first case. There isn't a shorthand method for the second case. But as Jesper noted, you can make your own.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Using java.util.regex.Pattern
 
Similar Threads
Beware of ctrl + c
Tech Word Game
SCEA part I certificate
T or F?
Sun Microsystems