tags:

views:

407

answers:

4

How do I match the following string?

http://localhost:8080/MenuTest/index.action

The regex should return true if it contains "MenuTest" in the above pattern.

Cheers

A: 

Something along these lines? (untested)

Regex r = new Regex("MenuTest");
r.search("http://localhost:8080/MenuTest/index.action");
System.out.println(""+r.didMatch());
miccet
eh? Regex isn't part of the standard Java API - Which 3rd party library is this from?
toolkit
I didn't realize, but it's using com.stevesoft.pat. Doesn't really matter what regex package (you have to use one, since you say it's not part of the standard API) you use, the idea is the same, to match a single word in a string.
miccet
java.util.regex has been part of the Java library for around seven years. No need for a third-party library where the standard will do.
Tom Hawtin - tackline
+9  A: 

Maybe you don't need a regex?

String url = "http://localhost:8080/MenuTest/index.action";    
boolean hasWhatImLookingFor = url.contains("MenuTest");
karim79
Use lowercase for variables: String url = "http://.....";
OscarRyz
@Oscar - This is true. Fixed.
karim79
@karim: Now you have to use that lowercase name! :) s/URL.contains/url.contains/ otherwise it will be worst as it was before. BTW +1 for the "simple" approach answer :)
OscarRyz
A: 

The regex you seek is simply "MenuTest", without the quotes. Unless the question is more complex than it appears?

Andrew Swan
+1  A: 

If you need to check if "MenuTest" appears at that specific position (i.e. after the host/port), then you can use:

^https?://[^/]++/MenuTest
Peter Boughton
Nice and precise solution :)
patjbs