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 :confused:
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());
}
}
}
}
Re: Change pattern of an existing pattern object?
Ok now im not getting a pattern match. :confused:
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());
}
}
}
}
Re: Change pattern of an existing pattern object?
Quote:
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.
Re: Change pattern of an existing pattern object?
Quote:
posted by CornedBee
You mean Pattern.compile, that's the reason for the uninitialized error.
Der. How dumb of me. :blush: Thanks for the help. :thumb: