|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.pf.text.AStringFilter
org.pf.text.StringPattern
public class StringPattern
This class provides services for checking strings against string-patterns.
Currently it supports the wildcards
'*' for any number of any character and
'?' for any single character.
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.
| Constructor Summary | |
|---|---|
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. |
|
| Method Summary | |
|---|---|
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. |
boolean |
equals(java.lang.Object obj)
Returns true if the given object is equal to the receiver. |
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. |
char |
getSingleCharWildcard()
Returns the wildcard character that is used as placeholder for a single occurance of any character. |
int |
hashCode()
Returns a hash code value for the object. |
boolean |
hasWildcard()
Returns true if the pattern contains any '*' or '?' |
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. |
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 ('*'). |
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. |
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. |
java.lang.String |
toString()
Returns the pattern string. |
| Methods inherited from class org.pf.text.AStringFilter |
|---|
matches |
| Methods inherited from class java.lang.Object |
|---|
getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
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)| Method Detail |
|---|
public boolean getIgnoreCase()
public void setIgnoreCase(boolean newValue)
public java.lang.String getPattern()
public void setPattern(java.lang.String newValue)
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 static StringPattern create(java.lang.String pattern)
pattern - The pattern to check against ( May contain '*' and '?' wildcards )
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 digits
public 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 digits
public 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 matches(java.lang.String probe)
matches in interface StringFilterprobe - The string to compare to the pattern
public 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 toString()
toString in class java.lang.ObjectObject.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.ObjectObject.equals(java.lang.Object)public int hashCode()
hashCode in class java.lang.ObjectObject.hashCode()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||