Pattern object using a factory method,
compile() of the
This is a static method, so you invoke it using the class, not an object:
Pattern pat = Pattern.compile( "x+y+");
A factory method (such as
compile()) is a static method of a class
that creates an object.
It is like a constructor, but is not used with the
It has a name other than the class name.
Here are the two ways to call
public static Pattern compile(String regex)
Patternobject based on the regular expression in regex. It throws a
PatternSyntaxExceptionif the expression's syntax is invalid.
public static Pattern compile(String regex, int flags)
Patternobject based on the regular expression in regex. Various options are chosen by setting various bits in
flags. It throws a
PatternSyntaxExceptionif the expression's syntax is invalid. It throws a
Details about the flags and the exceptions are discussed below. For now, assume that the program will halt (and produce a stack trace) if the expression or the flags are incorrect.
Does the following look correct?
Pattern pets = Pattern.compile( "ant|bat|cat|dog" );