String.matches vs Pattern and Matcher object

Hi,

I was trying to match some regex using String.matches but for me it is not working (probably I am not using it the way it should be used).

Here is a simple example:

/* This does not work */

String patternStr = "a";

String inputStr = "abc";

if(inputStr.matches( "a" ))

System.out.println("String matched");

/* This works */

Pattern p = Pattern.compile( "a" );

Matcher m = p.matcher( "abc" );

boolean found = false;

while(m.find())

{

System.out.println("Matched using Pattern and Matcher");

found = true;

}

if(!found)

{

System.out.println("Not matching with Pattern and Matcher");

}

Am I not matches method of String class properly?

Please throw some lights on this.

Thank you.

[826 byte] By [DeepSofta] at [2007-11-26 17:49:52]
# 1
String.matches looks at the whole string.bsh % "abc".matches("a");<false>bsh % "abc".matches("a.*");<true>
jverda at 2007-7-9 5:02:22 > top of Java-index,Java Essentials,Java Programming...