Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
itsolution@gmail.com - 14 Jun 2006 08:15 GMT
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?
Gordon Beaton - 14 Jun 2006 08:30 GMT
> 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

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Gijs Peek - 14 Jun 2006 08:30 GMT
Shouldn't you construct patterns like this:
Pattern p = Pattern.compile("dialer%instance%.+%name");

> Pattern p=new Pattern("dialer%instance%.+%name");
> Matcher matcher=p.matcher("dialer%instance%name");
> System.out.print(matcher.matches())
Robert Klemme - 14 Jun 2006 08:31 GMT
> 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
itsolution@gmail.com - 14 Jun 2006 17:50 GMT
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?

> Hi,
>
[quoted text clipped - 5 lines]
> or  "dialer%instance%1%name" would only match.  But,  why
> "dialer%instance%name" also matches  the pattern specified?
Robert Klemme - 14 Jun 2006 18:00 GMT
> 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
Hendrik Maryns - 14 Jun 2006 18:21 GMT
itsolution@gmail.com schreef:
> Oops, some errta correction.
>
[quoted text clipped - 5 lines]
> 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
Patricia Shanahan - 14 Jun 2006 19:00 GMT
> Oops, some errta correction.
>
[quoted text clipped - 5 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.