public class Pattern extends Object implements Serializable, REFlags
myExpr against a text myString one should first
create a Pattern object:Pattern p = new Pattern(myExpr);or
Pattern p = Pattern.compile(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 not thread-safe, and neither are Matcher objects.
REFlags,
Matcher,
Matcher.setTarget(java.lang.CharSequence),
Matcher.setTarget(java.lang.CharSequence, int, int),
Matcher.setTarget(char[], int, int),
Matcher.setTarget(java.io.Reader, int),
MatchResult,
MatchResult.group(int),
MatchResult.start(int),
MatchResult.end(int),
MatchResult.length(int),
MatchResult.charAt(int, int),
MatchResult.prefix(),
MatchResult.suffix(),
Serialized FormDEFAULT, DOTALL, IGNORE_CASE, IGNORE_SPACES, MULTILINE, UNICODE, XML_SCHEMA| Modifier | Constructor and Description |
|---|---|
protected |
Pattern() |
|
Pattern(String regex)
Compiles an expression with default flags.
|
|
Pattern(String regex,
String flags)
Compiles a regular expression using Perl5-style flags.
|
| Modifier and Type | Method and Description |
|---|---|
static Pattern |
compile(String regex)
Compiles the given String into a Pattern that can be used to match text.
|
static Pattern |
compile(String regex,
int flags)
Compiles the given String into a Pattern that can be used to match text.
|
static Pattern |
compile(String regex,
String flags)
Compiles the given String into a Pattern that can be used to match text.
|
boolean |
equals(Object o) |
int |
groupCount()
How many capturing groups does this expression include?
|
Integer |
groupId(String name)
Get numeric id for a group name.
|
int |
hashCode() |
Matcher |
matcher()
Returns a target-less matcher.
|
Matcher |
matcher(char[] data,
int start,
int end)
Returns a matcher for a specified region.
|
Matcher |
matcher(CharSequence s)
Returns a matcher for a specified string.
|
Matcher |
matcher(MatchResult res,
int groupId)
Returns a matcher for a match result (in a performance-friendly way).
|
Matcher |
matcher(MatchResult res,
String groupName)
Just as above, yet with symbolic group name.
|
Matcher |
matcher(Reader text,
int length)
Returns a matcher taking a text stream as target.
|
boolean |
matches(String s)
A shorthand for Pattern.matcher(String).matches().
|
Replacer |
replacer(String expr)
Returns a replacer of a pattern by specified perl-like expression.
|
Replacer |
replacer(Substitution model)
Returns a replacer will substitute all occurrences of a pattern
through applying a user-defined substitution model.
|
boolean |
startsWith(String s)
A shorthand for Pattern.matcher(String).matchesPrefix().
|
RETokenizer |
tokenizer(char[] data,
int off,
int len)
Tokenizes a specified region by an occurrences of the pattern.
|
RETokenizer |
tokenizer(Reader in,
int length)
Tokenizes a specified region by an occurrences of the pattern.
|
RETokenizer |
tokenizer(String text)
Tokenizes a text by an occurrences of the pattern.
|
String |
toString_d()
Returns a less or more readable representation of a bytecode for the pattern.
|
String |
toString() |
protected Pattern() throws PatternSyntaxException
PatternSyntaxExceptionpublic Pattern(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(String regex, 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 static Pattern compile(String regex) throws PatternSyntaxException
regex - a String in normal Java regular expression formatPatternSyntaxExceptionpublic static Pattern compile(String regex, int flags) throws PatternSyntaxException
regex - a String in normal Java regular expression formatflags - integer flags that are constructed via bitwise OR from the flag constants in REFlags.PatternSyntaxExceptionpublic static Pattern compile(String regex, String flags) throws PatternSyntaxException
regex - a String in normal Java regular expression formatflags - integer flags that are constructed via bitwise OR from the flag constants in REFlags.PatternSyntaxExceptionpublic int groupCount()
public Integer groupId(String name)
null if no such name found.MatchResult.group(java.lang.String),
MatchResult.isCaptured(java.lang.String)public boolean matches(String s)
s - the targetMatcher.matches(),
Matcher.matches(String)public boolean startsWith(String s)
s - the targetMatcher.matchesPrefix()public Matcher matcher(CharSequence 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, String groupName)
NullPointerException - if there is no group with such namepublic Matcher matcher(Reader 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 problempublic Replacer replacer(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.Replacerpublic Replacer replacer(Substitution model)
model - a Substitution object which is in charge for match substitutionReplacerpublic RETokenizer tokenizer(String text)
public RETokenizer tokenizer(char[] data, int off, int len)
public RETokenizer tokenizer(Reader in, int length) throws IOException
public String toString_d()
Copyright © 2016. All rights reserved.