public final class Pattern2 extends Object
java.util.regex.Pattern
class with a new WILDCARD
compilation flag, which modifies the pattern compilation such that it combines the well-known
wildcard pattern matching with the power of regular
expressions.compile(String, int)
Modifier and Type | Field and Description |
---|---|
static int |
WILDCARD
Modifies the pattern compilation as follows:
The meaning of the '*' and '?'
|
Modifier and Type | Method and Description |
---|---|
static Pattern |
compile(String pattern,
int flags)
Like
Pattern.compile(String,int) , but with support for the WILDCARD flag. |
static int |
findMeta(String metaCharacters,
String subject,
int offset)
Finds the next unescaped occurrence of one of the metaCharacters within subject, starting
at position offset.
|
static String[] |
parsePatternAndReplacement(String pattern)
Splits the given string into "pattern" and "replacement".
|
public static final int WILDCARD
The meaning of the '*' and '?' metacharacters is now different, and '.' is no longer a metacharacter.
'/' is now a metacharacter, i.e. to include it literally in the pattern, it must be escaped with a backslash.
The semantics of '*', '?' and '.' are as follows:
Construct | Matches |
---|---|
Wildcards | |
* | Zero or more characters except '/', the file separator and '!' |
** | Zero or more characters except '!' |
*** | Zero or more characters |
? | Any character except '/', the file separator and '!' |
. | The '.' |
/ |
'/' or the system-dependent file separator (see separatorChar )
|
{0,}
'. Similarly, to quantify 'zero or one', you can no longer write '?
', but
must use '{0,1}
'.public static Pattern compile(String pattern, int flags)
Pattern.compile(String,int)
, but with support for the WILDCARD
flag.
Notice that iff WILDCARD
is given, then Object.toString()
returns the regular expression
that was generated from the wildcard pattern (and not the wildcard pattern, as you'd probably expect).
public static String[] parsePatternAndReplacement(String pattern)
Example:
"(foo)=$1.bak"
results in { "(foo)", "$1.bak" }
.
Iff there is no non-escaped equals sign, then the resulting replacement is null
.
{ pattern, replacement }
public static int findMeta(String metaCharacters, String subject, int offset)
\Q ... \E
'.subject.length()
iff no meta character is foundCopyright © 2018 Arno Unkrig. All rights reserved.