public class StringPattern extends AStringFilter implements java.io.Serializable, IJSONConvertible, IStringRepresentation
The API is very simple:
There are two class methods match() and matchIgnoreCase().
Example:
StringPattern.match( 'Hello World", "H* W*" ) ; --> evaluates to true
StringPattern.matchIgnoreCase( 'StringPattern", "str???pat*" ) ; --> evaluates to true
To be compatible with most pattern engines this class also supports that the
multi-char wildcard '*' matches empty string. By default it doesn't !
To switch on this behaviour call
pattern.multiCharWildcardMatchesEmptyString(true);
If this option is set to true, the following example returns true,
otherwise it would return false:
StringPattern pattern = new StringPattern( "Fred*" ) ;
pattern.multiCharWildcardMatchesEmptyString(true);
return pattern.matches( "Fred" ) ; // <== returns true
It is also possible to instantiate new pattern object by using the static create() methods rather than the constructors. These methods will create new patterns that have the multiCharWildcardMatchesEmptyString option initialized to true.
Modifier and Type | Field and Description |
---|---|
protected static char |
DEFAULT_MULTICHAR_WILDCARD |
protected static char |
DEFAULT_SINGLECHAR_WILDCARD |
protected static java.lang.String |
SWITCH_OFF |
protected static java.lang.String |
SWITCH_ON |
JSON_ARRAY_END, JSON_ARRAY_START, JSON_ELEMENT_SEPARATOR, JSON_LITERAL_FALSE, JSON_LITERAL_NULL, JSON_LITERAL_TRUE, JSON_OBJECT_END, JSON_OBJECT_START, JSON_PAIR_SEPARATOR, JSON_STRING_DELIMITER, JSON_STRING_ESCAPE
ALL, NONE
Constructor and Description |
---|
StringPattern(java.lang.String pattern)
Initializes the new instance with the string pattern.
|
StringPattern(java.lang.String pattern,
boolean ignoreCase)
Initializes the new instance with the string pattern and the selection,
if case should be ignored when comparing characters.
|
StringPattern(java.lang.String pattern,
boolean ignoreCase,
char digitWildcard)
Initializes the new instance with the string pattern and the selection,
if case should be ignored when comparing characters plus a wildcard
character for digits.
|
StringPattern(java.lang.String pattern,
char digitWildcard)
Initializes the new instance with the string pattern and a digit wildcard
character.
|
Modifier and Type | Method and Description |
---|---|
void |
appendAsJSONString(java.lang.Appendable output)
The receiver of this method must append its internal state as JSON string
representation to the given buffer.
|
java.lang.String |
asString()
Returns the string representation of the implementing object.
|
protected boolean |
charsAreEqual(char probeChar,
char patternChar) |
static boolean |
containsWildcard(java.lang.String aString)
Returns true if the given string contains a single or multi character
wildcard.
|
StringPattern |
copy()
Returns a copy of this pattern.
|
static StringPattern |
create(java.lang.String pattern)
Returns a new instance with the string pattern.
|
static StringPattern |
create(java.lang.String pattern,
boolean ignoreCase)
Returns a new instance with the string pattern and the selection,
if case should be ignored when comparing characters.
|
static StringPattern |
create(java.lang.String pattern,
boolean ignoreCase,
char digitWildcard)
Returns a new instance with the string pattern and the selection,
if case should be ignored when comparing characters plus a wildcard
character for digits.
|
static StringPattern |
create(java.lang.String pattern,
char digitWildcard)
Returns anew instance with the string pattern and a digit wildcard
character.
|
protected java.lang.Character |
digitWildcard() |
protected void |
digitWildcard(java.lang.Character newValue) |
protected char |
digitWildcardChar() |
protected boolean |
endNotReached(char character) |
protected boolean |
endReached(char character) |
boolean |
equals(java.lang.Object obj)
Returns true if the given object is equal to the receiver.
|
protected boolean |
eventuallyMatched(char probeCh,
char patternCh,
StringExaminer patternIterator) |
static char |
getDefaultMultiCharWildcard()
Returns the character that is used to specify any number of any character.
|
static char |
getDefaultSingleCharWildcard()
Returns the character that is used to specify any single character.
|
boolean |
getIgnoreCase()
Returns whether or not the pattern matching ignores upper and lower case
|
char |
getMultiCharWildcard()
Returns the wildcard character that is used as placeholder for zero to many
occurances of any character(s).
|
java.lang.String |
getPattern()
Returns the pattern as string.
|
protected char |
getPatternChar(StringExaminer patternIterator,
char probeCh) |
char |
getSingleCharWildcard()
Returns the wildcard character that is used as placeholder for a single
occurance of any character.
|
protected boolean |
hasDigitWildcard() |
int |
hashCode()
Returns a hash code value for the object.
|
boolean |
hasWildcard()
Returns true if the pattern contains any '*' or '?' or
digit wildcard wildcard character.
|
protected java.lang.String |
ignoreCaseAsString() |
protected java.lang.String |
inspectString() |
static boolean |
match(java.lang.String probe,
java.lang.String pattern)
Returns true, if the given probe string matches the given pattern.
|
boolean |
matches(java.lang.String probe)
Tests if a specified string matches the pattern.
|
static boolean |
matchIgnoreCase(java.lang.String probe,
java.lang.String pattern)
Returns true, if the given probe string matches the given pattern.
|
protected boolean |
matchReverse(java.lang.String patternStr,
StringExaminer probeIterator) |
boolean |
multiCharWildcardMatchesEmptyString()
Returns true, if this StringPattern allows empty strings at the position
of the multi character wildcard ('*').
|
void |
multiCharWildcardMatchesEmptyString(boolean newValue)
sets whether or not this StringPattern allows empty strings at the
position of the multi character wildcard ('*').
|
protected StringExaminer |
newExaminer(java.lang.String str) |
java.lang.String[] |
reject(java.lang.String[] strings)
Returns an array containing all of the given strings that do NOT match
this pattern.
|
java.lang.String[] |
select(java.lang.String[] strings)
Returns an array containing all of the given strings that match this
pattern.
|
protected java.lang.String[] |
selectOrReject(java.lang.String[] strings,
boolean select) |
void |
setDigitWildcardChar(char digitWildcard)
Sets the given character as a wildcard character in this pattern to
match only digits ('0'-'9').
|
void |
setIgnoreCase(boolean newValue)
Sets whether the pattern matching should ignore case or not
|
void |
setMultiCharWildcard(char newValue)
Sets the wildcard character that is used as placeholder for zero to many
occurances of any character(s).
|
void |
setPattern(java.lang.String newValue)
Sets the pattern to a new value
|
void |
setSingleCharWildcard(char newValue)
Sets the wildcard character that is used as placeholder for a single
occurance of any character.
|
protected boolean |
skipAfter(StringExaminer examiner,
java.lang.String matchString)
Increments the given iterator up to the last character that matched
the character sequence in the given matchString.
|
protected char |
skipWildcards(StringExaminer iterator)
Moves the iterator position to the next character that is no wildcard.
|
protected StringUtil |
strUtil() |
java.lang.String |
toJSON()
Returns a JSON string representation of this object.
|
java.lang.String |
toString()
Returns the pattern string.
|
protected java.lang.String |
upToEnd(StringExaminer iterator) |
protected static final char DEFAULT_MULTICHAR_WILDCARD
protected static final char DEFAULT_SINGLECHAR_WILDCARD
protected static final java.lang.String SWITCH_ON
protected static final java.lang.String SWITCH_OFF
public StringPattern(java.lang.String pattern, boolean ignoreCase)
pattern
- The pattern to check against ( May contain '*' and '?' wildcards )ignoreCase
- Definition, if case sensitive character comparison or not.create(String, boolean)
public StringPattern(java.lang.String pattern)
As an alternative see also StringPattern.create() method.
pattern
- The pattern to check against ( May contain '*' and '?' wildcards )create(String)
public StringPattern(java.lang.String pattern, char digitWildcard)
pattern
- The pattern to check against ( May contain '*', '?' wildcards and the digit wildcard )digitWildcard
- A wildcard character that stands as placeholder for digitscreate(String, char)
public StringPattern(java.lang.String pattern, boolean ignoreCase, char digitWildcard)
pattern
- The pattern to check against ( May contain '*' and '?' wildcards )ignoreCase
- Definition, if case sensitive character comparison or not.digitWildcard
- A wildcard character that stands as placeholder for digitscreate(String, boolean, char)
public static StringPattern create(java.lang.String pattern, boolean ignoreCase)
pattern
- The pattern to check against ( May contain '*' and '?' wildcards )ignoreCase
- Definition, if case sensitive character comparison or not.public static StringPattern create(java.lang.String pattern, char digitWildcard)
pattern
- The pattern to check against ( May contain '*', '?' wildcards and the digit wildcard )digitWildcard
- A wildcard character that stands as placeholder for digitspublic static StringPattern create(java.lang.String pattern, boolean ignoreCase, char digitWildcard)
Example:
StringPattern.create( "*London*Eye#", true, '#' ).matches( "londonEYE8" ) ==> true
pattern
- The pattern to check against ( May contain '*' and '?' wildcards )ignoreCase
- Definition, if case sensitive character comparison or not.digitWildcard
- A wildcard character that stands as placeholder for digitspublic static boolean match(java.lang.String probe, java.lang.String pattern)
probe
- The string to check against the pattern.pattern
- The patter, that probably contains wildcards ( '*' or '?' )public static boolean matchIgnoreCase(java.lang.String probe, java.lang.String pattern)
probe
- The string to check against the pattern.pattern
- The patter, that probably contains wildcards ( '*' or '?' )public static char getDefaultMultiCharWildcard()
public static char getDefaultSingleCharWildcard()
public static boolean containsWildcard(java.lang.String aString)
aString
- The string to check for wildcard characterspublic boolean getIgnoreCase()
public void setIgnoreCase(boolean newValue)
public java.lang.String getPattern()
public void setPattern(java.lang.String newValue)
public static StringPattern create(java.lang.String pattern)
pattern
- The pattern to check against ( May contain '*' and '?' wildcards )public char getSingleCharWildcard()
public void setSingleCharWildcard(char newValue)
public char getMultiCharWildcard()
public void setMultiCharWildcard(char newValue)
public boolean multiCharWildcardMatchesEmptyString()
The default value is false.
public void multiCharWildcardMatchesEmptyString(boolean newValue)
The default value is false.
public boolean matches(java.lang.String probe)
matches
in interface IObjectFilter<java.lang.String>
matches
in interface IStringFilter
probe
- The string to compare to the patternpublic java.lang.String[] select(java.lang.String[] strings)
strings
- The strings to be matched against this patternpublic java.lang.String[] reject(java.lang.String[] strings)
strings
- The strings to be matched against this patternpublic java.lang.String asString()
IStringRepresentation
asString
in interface IStringRepresentation
public java.lang.String toString()
toString
in class java.lang.Object
Object.toString()
public boolean hasWildcard()
public void setDigitWildcardChar(char digitWildcard)
digitWildcard
- The placeholder character for digitspublic StringPattern copy()
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
Object.equals(java.lang.Object)
public int hashCode()
hashCode
in class java.lang.Object
Object.hashCode()
public void appendAsJSONString(java.lang.Appendable output)
IJSONConvertible
appendAsJSONString
in interface IJSONConvertible
output
- The output target to which to append the JSON string (must not be null).public java.lang.String toJSON()
toJSON
in interface IJSONConvertible
protected boolean eventuallyMatched(char probeCh, char patternCh, StringExaminer patternIterator)
protected boolean hasDigitWildcard()
protected char digitWildcardChar()
protected char skipWildcards(StringExaminer iterator)
protected boolean skipAfter(StringExaminer examiner, java.lang.String matchString)
matchString
- The string to be found (must not contain *)protected java.lang.String upToEnd(StringExaminer iterator)
protected boolean matchReverse(java.lang.String patternStr, StringExaminer probeIterator)
protected boolean charsAreEqual(char probeChar, char patternChar)
protected boolean endReached(char character)
protected boolean endNotReached(char character)
protected char getPatternChar(StringExaminer patternIterator, char probeCh)
protected StringExaminer newExaminer(java.lang.String str)
protected java.lang.String ignoreCaseAsString()
protected java.lang.String[] selectOrReject(java.lang.String[] strings, boolean select)
protected java.lang.String inspectString()
protected StringUtil strUtil()
protected java.lang.Character digitWildcard()
protected void digitWildcard(java.lang.Character newValue)