public class Pattern extends java.lang.Object implements REFlags
myExpr
against a text myString
one should first create a Pattern object:Pattern p=new Pattern(myExpr);then obtain a Matcher object:
Matcher matcher=p.matcher(myText);The latter is an automaton that actually performs a search. It provides the following methods:
Flags
Flags (see REFlags interface) change the meaning of some regular expression elements at compiletime.
These flags may be passed both as string(see Pattern(String,String)) and as bitwise OR of:
Multithreading
Pattern instances are thread-safe, i.e. the same Pattern object may be used
by any number of threads simultaniously. On the other hand, the Matcher objects
are NOT thread safe, so, given a Pattern instance, each thread must obtain
and use its own Matcher.
REFlags
,
Matcher
,
Matcher.setTarget(java.lang.String)
,
Matcher.setTarget(java.lang.String,int,int)
,
Matcher.setTarget(char[],int,int)
,
MatchResult
,
MatchResult.group(int)
,
MatchResult.start(int)
,
MatchResult.end(int)
,
MatchResult.length(int)
,
MatchResult.charAt(int,int)
,
MatchResult.prefix()
,
MatchResult.suffix()
DEFAULT, DOTALL, IGNORE_CASE, IGNORE_SPACES, MULTILINE, UNICODE, XML_SCHEMA
Modifier | Constructor and Description |
---|---|
protected |
Pattern() |
|
Pattern(java.lang.String regex)
Compiles an expression with default flags.
|
|
Pattern(java.lang.String regex,
int flags)
Compiles a regular expression using REFlags.
|
|
Pattern(java.lang.String regex,
java.lang.String flags)
Compiles a regular expression using Perl5-style flags.
|
Modifier and Type | Method and Description |
---|---|
static Pattern |
compile(java.lang.String regex) |
static Pattern |
compile(java.lang.String regex,
int flags) |
protected void |
compileInt(java.lang.String regex,
int flags) |
int |
groupCount()
How many capturing groups this expression includes?
|
java.lang.Integer |
groupId(java.lang.String name)
Get numeric id for a group name.
|
Matcher |
matcher()
Returns a targetless matcher.
|
Matcher |
matcher(char[] data,
int start,
int end)
Returns a matcher for a specified region.
|
Matcher |
matcher(CharStream text,
int length)
Returns a matcher taking a text stream as target.
|
Matcher |
matcher(MatchResult res,
int groupId)
Returns a matcher for a match result (in a performance-friendly way).
|
Matcher |
matcher(MatchResult res,
java.lang.String groupName)
Just as above, yet with symbolic group name.
|
Matcher |
matcher(java.lang.String s)
Returns a matcher for a specified string.
|
boolean |
matches(java.lang.String s)
A shorthand for Pattern.matcher(String).matches().
|
Replacer |
replacer(java.lang.String expr)
Returns a replacer of a pattern by specified perl-like expression.
|
Replacer |
replacer(Substitution model)
Returns a replacer will substitute all occurences of a pattern
through applying a user-defined substitution model.
|
boolean |
startsWith(java.lang.String s)
A shorthand for Pattern.matcher(String).matchesPrefix().
|
RETokenizer |
tokenizer(char[] data,
int off,
int len)
Tokenizes a specified region by an occurences of the pattern.
|
RETokenizer |
tokenizer(CharStream in,
int length)
Tokenizes a specified region by an occurences of the pattern.
|
RETokenizer |
tokenizer(java.lang.String text)
Tokenizes a text by an occurences of the pattern.
|
java.lang.String |
toString_d()
Returns a less or more readable representation of a bytecode for the pattern.
|
java.lang.String |
toString() |
protected Pattern() throws PatternSyntaxException
PatternSyntaxException
public Pattern(java.lang.String regex) throws PatternSyntaxException
regex
- the Perl5-compatible regular expression string.PatternSyntaxException
- if the argument doesn't correspond to perl5 regex syntax.Pattern(java.lang.String,java.lang.String)
,
Pattern(java.lang.String,int)
public Pattern(java.lang.String regex, java.lang.String flags) throws PatternSyntaxException
regex
- the Perl5-compatible regular expression string.flags
- the Perl5-compatible flags.PatternSyntaxException
- if the argument doesn't correspond to perl5 regex syntax.
see REFlagspublic Pattern(java.lang.String regex, int flags) throws PatternSyntaxException
flags
parameter is a bitwise OR of the folloing values:
regex
- the Perl5-compatible regular expression string.flags
- the Perl5-compatible flags.PatternSyntaxException
- if the argument doesn't correspond to perl5 regex syntax.
see REFlagspublic static Pattern compile(java.lang.String regex) throws PatternSyntaxException
PatternSyntaxException
public static Pattern compile(java.lang.String regex, int flags) throws PatternSyntaxException
PatternSyntaxException
protected void compileInt(java.lang.String regex, int flags) throws PatternSyntaxException
PatternSyntaxException
public int groupCount()
public java.lang.Integer groupId(java.lang.String name)
null
if no such name found.MatchResult.group(java.lang.String)
,
MatchResult.isCaptured(java.lang.String)
public boolean matches(java.lang.String s)
s
- the targetMatcher.matches()
,
Matcher.matches(String)
public boolean startsWith(java.lang.String s)
s
- the targetMatcher.matchesPrefix()
public Matcher matcher()
public Matcher matcher(java.lang.String s)
public Matcher matcher(char[] data, int start, int end)
public Matcher matcher(MatchResult res, int groupId)
groupId
parameter specifies which group is a target.groupId
- which group is a target; either positive integer(group id), or one of MatchResult.MATCH,MatchResult.PREFIX,MatchResult.SUFFIX,MatchResult.TARGET.public Matcher matcher(MatchResult res, java.lang.String groupName)
java.lang.NullPointerException
- if there is no group with such namepublic Matcher matcher(CharStream text, int length) throws IOException
text
- a text streamlength
- the length to read from a stream; if len
is -1
, the whole stream is read in.IOException
- indicates an IO problemjava.lang.OutOfMemoryError
- if a stream is too lengthypublic Replacer replacer(java.lang.String expr)
String text="The quick brown fox jumped over the lazy dog"; Pattern word=new Pattern("\\w+"); System.out.println(word.replacer("[$&]").replace(text)); //prints "[The] [quick] [brown] [fox] [jumped] [over] [the] [lazy] [dog]" Pattern swap=new Pattern("(fox|dog)(.*?)(fox|dog)"); System.out.println(swap.replacer("$3$2$1").replace(text)); //prints "The quick brown dog jumped over the lazy fox" Pattern scramble=new Pattern("(\\w+)(.*?)(\\w+)"); System.out.println(scramble.replacer("$3$2$1").replace(text)); //prints "quick The fox brown over jumped lazy the dog"
expr
- a perl-like expression, the "$&" and "${&}" standing for whole match, the "$N" and "${N}" standing for group#N, and "${Foo}" standing for named group Foo.Replacer
public Replacer replacer(Substitution model)
model
- a Substitution object which is in charge for match substitutionReplacer
public RETokenizer tokenizer(java.lang.String text)
public RETokenizer tokenizer(char[] data, int off, int len)
public RETokenizer tokenizer(CharStream in, int length) throws IOException
IOException
RETokenizer
public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toString_d()