[Q]What's wrong with this Pattern matching code?

I

itsolution

Hi,

Pattern p=new Pattern("dialer%instance%.+%name");
Matcher matcher=p.matcher("dialer%instance%name");
System.out.print(matcher.matches()) ==> This prints 'true'!

How come, it matches? I expected e.g) "dialer%instance%1%name"
or "dialer%instance%1%name" would only match. But, why
"dialer%instance%name" also matches the pattern specified?
 
G

Gordon Beaton

Pattern p=new Pattern("dialer%instance%.+%name");
Matcher matcher=p.matcher("dialer%instance%name");
System.out.print(matcher.matches()) ==> This prints 'true'!

How come, it matches?

Hard to say, since the code you tested isn't the code you posted.

For example, there is no public constructor for Pattern. After making
that correction, I get "false" as expected.

Post real, compilable code if you want a useful answer.

I expected e.g) "dialer%instance%1%name" or "dialer%instance%1%name"
would only match.

Did you mean to post two identical examples here?

But, why "dialer%instance%name" also matches the pattern specified?

It doesn't.

/gordon
 
G

Gijs Peek

Shouldn't you construct patterns like this:
Pattern p = Pattern.compile("dialer%instance%.+%name");
 
R

Robert Klemme

Hi,

Pattern p=new Pattern("dialer%instance%.+%name");
Matcher matcher=p.matcher("dialer%instance%name");
System.out.print(matcher.matches()) ==> This prints 'true'!
No.

How come, it matches? I expected e.g) "dialer%instance%1%name"
or "dialer%instance%1%name" would only match. But, why
"dialer%instance%name" also matches the pattern specified?

You're confused. Your code doesn't even compile.

robert
 
I

itsolution

Oops, some errta correction.

Pattern p=new Pattern.compile("dialer%instance%.+%name");
Matcher matcher=p.matcher("dialer%instance%name");
System.out.print(matcher.matches()) ==> This prints 'true'!

How come, it matches? I expected e.g) "dialer%instance%1%name"
or "dialer%instance%2%name" would only match. But, why
"dialer%instance%name" also matches the pattern specified?
 
R

Robert Klemme

Oops, some errta correction.

Pattern p=new Pattern.compile("dialer%instance%.+%name");
Matcher matcher=p.matcher("dialer%instance%name");
System.out.print(matcher.matches()) ==> This prints 'true'!

You're still confused. Go away.

robert
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) schreef:
Oops, some errta correction.

Pattern p=new Pattern.compile("dialer%instance%.+%name");
Matcher matcher=p.matcher("dialer%instance%name");
System.out.print(matcher.matches()) ==> This prints 'true'!

How come, it matches? I expected e.g) "dialer%instance%1%name"
or "dialer%instance%2%name" would only match. But, why
"dialer%instance%name" also matches the pattern specified?

You code still does not compile. Remove ‘new’, and try it out first if
you post again in the future.

H.

- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEkEWde+7xMGD3itQRAsxpAJ9eIO4bCyf/o7BxyxMXBA+hrtyXNACdHPgU
TwHB6JQgJSL9gRuRqESYC44=
=Uayp
-----END PGP SIGNATURE-----
 
P

Patricia Shanahan

Oops, some errta correction.

Pattern p=new Pattern.compile("dialer%instance%.+%name");
Matcher matcher=p.matcher("dialer%instance%name");
System.out.print(matcher.matches()) ==> This prints 'true'!

How come, it matches? I expected e.g) "dialer%instance%1%name"
or "dialer%instance%2%name" would only match. But, why
"dialer%instance%name" also matches the pattern specified?

Rather than posting errata, it seems to be time for a Short,
Self-Contained, Compilable Example (SSCCE). I tried to construct one
from the snippet you posted. I had edit out "new " to get it to compile,
so I know what you posted is not what you are running. This program
prints "false", so some difference from what you are running is
significant.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternTest {
public static void main(String[] args) {
Pattern p=Pattern.compile("dialer%instance%.+%name");
Matcher matcher=p.matcher("dialer%instance%name");
System.out.print(matcher.matches());
}
}

Your best bet for getting help is to edit this example to make it match
what you are doing, and post the new version. Make sure you copy-paste,
with no retyping, when you post it, so that the program in the message
is EXACTLY the program you are running.

Keep the form of a very short but complete program. Anyone who wants to
help you should be able to copy-paste the program from your message into
their favorite Java editor and reproduce your problem without having to
add, delete, or change a single byte.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top