This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Pattern.matches woes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pattern.matches woes" Watch "Pattern.matches woes" New topic

Pattern.matches woes

ilteris kaplan
Ranch Hand

Joined: Jan 21, 2006
Posts: 38
Hello, I am using regex to check some links on my code and basically I I want to know whether they have certain words in those urls.

The way I am checking this is below:

apparently matches tries to match for the whole newurl to google. I tested it by changing the newurl = "google" and it returned me true. so can someone address me what I am doing wrong here?

thanks a lot.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Instead of using Pattern.match() which does match the entire string, why not just use another Matcher and its find() method to see if part of the string matches the term?

The other option is to use a regex pattern that will catch any prefix or suffix to the desired pattern. So .*google.* as an example.
ilteris kaplan
Ranch Hand

Joined: Jan 21, 2006
Posts: 38
working like a charm.
Don't get me started about those stupid light bulbs.
subject: Pattern.matches woes