aspose file tools
The moose likes Java in General and the fly likes new to regex, pattern behavior question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "new to regex, pattern behavior question" Watch "new to regex, pattern behavior question" New topic
Author

new to regex, pattern behavior question

Julia Reynolds
Ranch Hand

Joined: May 31, 2001
Posts: 123
How come this regex pattern comparison returns false?

Pattern pt = Pattern.compile("CDE");
System.out.println("Pattern.matches(\"CDE\", \"ABCDEFGH\"):" + pt.matcher("ABCDEFGH").matches() );

I don't understand why I don't get a match on 'CDE'.

Julia
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Your regular expression only matches exactly "CDE", not strings containing "CDE". If you want the latter, I think your expression should be ".*CDE.*" . That is, zero or more characters (of any type), followed by "CDE", followed by zero or more characters (of any type).


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Julia Reynolds
Ranch Hand

Joined: May 31, 2001
Posts: 123
Peter,

Aha! You're right, of course. Thanks!

Julia
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Julia, you might also like to take a look at other methods in the regex API.

Pattern pt = Pattern.compile("CDE");
System.out.println("Pattern.matches(\"CDE\", \"ABCDEFGH\"):" + pt.matcher("ABCDEFGH").find() );

In this case, the result is true.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Julia Reynolds
Ranch Hand

Joined: May 31, 2001
Posts: 123
Dirk -

Thanks, good advice.

J
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: new to regex, pattern behavior question
 
Similar Threads
IDE's for Redhat 6.2 ?
String replaceAll()
Making strings safe for regex
Regex in Java: finding expression 1, but not if it's part of expression 2
If only one vowel is entered in the string, then that vowel should be diplayed on console