Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: java.util.r...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 3 Topic 16066 of 16269
Post > Topic >>

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

by Knute Johnson <nospam@[EMAIL PROTECTED] > May 8, 2008 at 03:30 PM

phillip.s.powell@[EMAIL PROTECTED]
 wrote:
> <%
> 
>    String stuff = "<c:im****t 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

 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.

im****t java.util.regex.*;

public class test {
     public static void main(String[] args) {
         String stuff =
          "<c:im****t 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:\Do***ents and Settings\Knute Johnson>java test
found
start=28
end=37

-- 

Knute Johnson
email s/nospam/linux/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
      ------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 




 3 Posts in Topic:
java.util.regex.Pattern method matches() fails in this context
"phillip.s.powell@[E  2008-05-08 13:41:11 
Re: java.util.regex.Pattern method matches() fails in this conte
Knute Johnson <nospam@  2008-05-08 15:30:48 
Re: java.util.regex.Pattern method matches() fails in this conte
Roedy Green <see_websi  2008-05-10 07:48:19 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 5:05:14 CDT 2008.