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 / First Aid / May 2008

Tip: Looking for answers? Try searching our database.

java.util.regex.Pattern method matches() fails in this context

Thread view: 
phillip.s.powell@gmail.com - 08 May 2008 21:41 GMT
<%

  String stuff = "<c:import url=\"/common/lib\" context=\"common\" /
>";
  if (Pattern.matches("context=\"", stuff)) {
     out.println("the line has a match");
  } else {
     out.println("the line does not have a match");
  }

%>

The following bit of JSP appears to constantly fail no matter how many
times I run it.  I am simply looking for the pattern context=" and
replacing it with context="/  throughout an entire String value, but
my regular expression appears to be wrong.

Could someone help me figure out what the right expression would be?

Phil
Knute Johnson - 08 May 2008 23:30 GMT
> <%
>
[quoted text clipped - 16 lines]
>
> Phil

From the docs;

 "A matcher is created from a pattern by invoking the pattern's matcher
method. Once created, a matcher can be used to perform three different
kinds of match operations:

    *

      The matches method attempts to match the entire input sequence
against the pattern.
    *

      The lookingAt method attempts to match the input sequence,
starting at the beginning, against the pattern.
    *

      The find method scans the input sequence looking for the next
subsequence that matches the pattern."

So your matches will always fail.  You can either modify your regex to
match the entire sequence or use the find() method which searches.

import java.util.regex.*;

public class test {
    public static void main(String[] args) {
        String stuff =
         "<c:import url=\"/common/lib\" context=\"common\" /> >";

        Pattern p = Pattern.compile("context=\"");
        Matcher m = p.matcher(stuff);

        if (m.find()) {
            System.out.println("found");
            System.out.println("start="+m.start());
            System.out.println("end="+m.end());
        }
    }
}

C:\Documents and Settings\Knute Johnson>java test
found
start=28
end=37

Signature

Knute Johnson
email s/nospam/linux/

Roedy Green - 10 May 2008 08:48 GMT
On Thu, 8 May 2008 13:41:11 -0700 (PDT), "phillip.s.powell@gmail.com"
<phillip.s.powell@gmail.com> wrote, quoted or indirectly quoted
someone who said :

> if (Pattern.matches("context=\"", stuff)) {

you are apparently confused between matching, finding and looking.

See http://mindprod.com/jgloss/regex.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



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



©2008 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.