public abstract class Glob extends Object implements Predicate<String>
java.util.regex.Pattern
and de.unkrig.commons.util.pattern.Pattern2
classes as follows:
Pattern
defines the both the interface of pattern matching and its implementation
(regular expressions). This makes it impossible to have different pattern matchers with the same interface.
Glob
is that new interface, and Glob.compile()
compiles a regular
expression into a Glob
, just like Pattern2.compile()
compiles it
into a Pattern
.
Glob
replaces the powerful (yet huge) API of java.util.regex
with a simple one: matches(String)
and replace(String)
. Pattern finding (as opposed to matching) and
repeated replacements are no longer supported. However, this approach is much more generic than (highly
regex-specific) concepts of "groups", "anchors", "lookaheads" and "lookbehinds".
REPLACEMENT
compilation flag modifies the pattern compilation such that a "replacement string"
can be defined in the pattern, which is often convenient.
INCLUDES_EXCLUDES
compilation flag modifies the pattern compilation such that a pattern can
be composed from a sequence of patterns, which are combined logically.
Modifier and Type | Field and Description |
---|---|
static Glob |
ANY
|
static int |
INCLUDES_EXCLUDES
Modifies the pattern compilation as follows:
',' and '~' are now metacharacters, i.e. to include them literally in the pattern, they must be escaped with a
backslash.
|
static Glob |
NONE
|
static int |
REPLACEMENT
Modifies the pattern compilation as follows:
'=' is now a metacharacter, i.e. to include it literally in the pattern, it must be escaped with a backslash.
|
Constructor and Description |
---|
Glob() |
Modifier and Type | Method and Description |
---|---|
static Glob |
and(Glob pattern1,
Glob pattern2)
The
matches(String) method of the returned Glob returns whether its subject
argument matches both pattern1 and pattern2. |
static Glob |
and(Glob pattern,
Predicate<? super String> predicate)
The
matches(String) method of the returned Glob returns whether the subject argument
matches the pattern and the predicate evaluates to true . |
static Glob |
and(Predicate<? super String> predicate,
Glob pattern)
The
matches(String) method of the returned Glob returns whether the predicate
evaluates to true and the subject argument matches the pattern. |
static Glob |
compile(String pattern)
Equivalent with
compile(regex, 0) . |
static Glob |
compile(String pattern,
int flags)
|
static Glob |
compileRegex(Pattern regex)
Returns a
Glob who's replace(String) method will return its subject argument if the
subject matches the given regex. |
static Glob |
compileRegex(Pattern regex,
String replacementString)
The behavior of the
matches(String) method of the returned Glob is as follows:
If the regex matches the subject, then true is returned. |
boolean |
evaluate(String subject)
Implementation of
PredicateWhichThrows.evaluate(T) ; calls {#matches}. |
static Glob |
fromPredicate(Predicate<? super String> predicate) |
abstract boolean |
matches(String subject) |
static Glob |
or(Glob pattern1,
Glob pattern2)
The
matches(String) method of the returned Glob returns whether its subject
argument matches pattern1 or, if not, pattern2. |
String |
replace(String subject)
|
public static final int REPLACEMENT
'=' is now a metacharacter, i.e. to include it literally in the pattern, it must be escaped with a backslash.
The semantics of '=' is as follows:
Replacement | a=b |
If a subject matches a, then the replace(String) method does not return the subject , but b. The replacement string b may contain references to captured subsequences as
in the Matcher.appendReplacement(java.lang.StringBuffer, java.lang.String) method.
|
---|
public static final int INCLUDES_EXCLUDES
',' and '~' are now metacharacters, i.e. to include them literally in the pattern, they must be escaped with a backslash.
The semantics of ',' and '~' are as follows:
Construct | Matches |
---|---|
Includes and excludes | |
a,b | Any subject that matches a or b |
a~b | Any subject that matches a, but not b |
REPLACEMENT
.public static final Glob ANY
public static final Glob NONE
public abstract boolean matches(String subject)
Glob
.public boolean evaluate(@Nullable String subject)
PredicateWhichThrows.evaluate(T)
; calls {#matches}.
If your code uses only Glob
and not Predicate
, you should favor calling matches(java.lang.String)
,
because that method name is more expressive.
A subject value null
evaluates to false
.
evaluate
in interface PredicateWhichThrows<String,NoException>
public static Glob compileRegex(Pattern regex)
Glob
who's replace(String)
method will return its subject argument if the
subject matches the given regex.public static Glob compileRegex(Pattern regex, @Nullable String replacementString)
matches(String)
method of the returned Glob
is as follows:
true
is returned.true
is returned. (Effectively, a glob 'dir' or 'dir/file.zip' matches all members
and entries under 'dir' resp. 'dir/file.zip'.)
true
is returned. (Example: The subject
"dir/"
is matched by regexs "dir"
, "dir/"
, "dir/anything"
and
"**.c"
, but not by regexs "dirr/anything"
, "file"
, "*.c"
and
"file.zip!file"
.)
false
is returned.replace(String)
method of the returned Glob
is as follows:
null
, then the subject is returned.regex
's "capturing groups" (see Pattern
).
public static Glob compile(String pattern)
compile(regex, 0)
.compile(String, int)
public static Glob compile(String pattern, int flags)
Pattern.compile(String, int)
, but returns a Glob
instead of a Pattern
.
Iff the flag REPLACEMENT
is set, then the pattern may include a "replacement".
Iff a replacement is specified, then replace(String)
will return the replacement, with
"$1
"... replaces with the match groups; otherwise the subject will be returned.
flags
- Modifies the semantics of the pattern, e.g. Pattern2.WILDCARD
switches from
regular expressions to wildcardsINCLUDES_EXCLUDES
,
REPLACEMENT
,
Pattern2.WILDCARD
,
Pattern.CANON_EQ
,
Pattern.CASE_INSENSITIVE
,
Pattern.COMMENTS
,
Pattern.DOTALL
,
Pattern.LITERAL
,
Pattern.MULTILINE
,
Pattern.UNICODE_CASE
,
Pattern.UNIX_LINES
public static Glob and(Glob pattern1, Glob pattern2)
matches(String)
method of the returned Glob
returns whether its subject
argument matches both pattern1 and pattern2.
The replace(String)
method of the returned Glob
returns checks whether the subject
matches pattern1; if so, it calls replace(String)
on pattern2 and returns the
result; otherwise it returns null
.
public static Glob or(Glob pattern1, Glob pattern2)
matches(String)
method of the returned Glob
returns whether its subject
argument matches pattern1 or, if not, pattern2.
The replace(String)
method of the returned Glob
returns calls replace(String)
on
pattern1 and returns the result if it is not null
; otherwise it calls replace(String)
on pattern2 and returns the result.
public static Glob and(Predicate<? super String> predicate, Glob pattern)
matches(String)
method of the returned Glob
returns whether the predicate
evaluates to true
and the subject argument matches the pattern.
The replace(String)
method of the returned Glob
returns checks whether the predicate
evaluates to true
; if so, it calls replace(String)
on pattern and returns the
result; otherwise it returns null
.
public static Glob and(Glob pattern, Predicate<? super String> predicate)
matches(String)
method of the returned Glob
returns whether the subject argument
matches the pattern and the predicate evaluates to true
.
The replace(String)
method of the returned Glob
returns checks whether the replace(String)
on the pattern returns nuon-null
and the predicate evaluates
to true
; if so, it returns the result of the replace(String)
call; otherwise it returns
null
.
Copyright © 2018 Arno Unkrig. All rights reserved.