Results 1 to 4 of 4

Thread: Change pattern of an existing pattern object?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Change pattern of an existing pattern object?

    Is there anyway that i could reuse an existing pattern object and just change the pattern without having to create a whole new pattern object by compiling it? Plus i am getting an error variable p might not have been initialized
    Code:
     import java.util.*;
     import java.util.regex.Pattern;
     import java.util.regex.Matcher;  
    
     public class X{
      public static void main(String[] args){
     
       String flavors = new String("marshmellowipeanutbutter   i   rockyroad");
       Pattern p = p.compile("\\s*i\\s*");
       // want to keep the same pattern object just change the pattern 
       Scanner s = new Scanner(flavors); //.useDelimiter("\\s*i\\s*"); 
       Matcher m = p.matcher(flavors);
       if(m.matches()){ 
        while(s.hasNext(p)){
         System.out.println(s.next()); 
        }
       }
      }
     }

  2. #2

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Change pattern of an existing pattern object?

    Ok now im not getting a pattern match.
    Code:
     import java.util.*;
     import java.util.regex.Pattern;
     import java.util.regex.Matcher;  
    
     public class X{
      public static void main(String[] args){
     
       String flavors = new String("marshmellowipeanutbutter   i   rockyroad");
       Pattern p = null; 
       p = p.compile("\\s*i\\s*");
       Scanner s = new Scanner(flavors); //.useDelimiter("\\s*i\\s*"); 
       Matcher m = p.matcher(flavors);
       if(m.matches()){ 
        while(s.hasNext(p)){
         System.out.println(s.next()); 
        }
       }
      }
     }

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Change pattern of an existing pattern object?

    Pattern p = p.compile("\\s*i\\s*");
    You mean Pattern.compile, that's the reason for the uninitialized error.

    And no, a compiled pattern is compiled to Java bytecode and can not be modified. Doing so would be far too complex and unreliable. You have to completely recompile it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Change pattern of an existing pattern object?

    posted by CornedBee

    You mean Pattern.compile, that's the reason for the uninitialized error.
    Der. How dumb of me. Thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width