📄 stringpattern.java
字号:
// ===========================================================================
// CONTENT : CLASS StringPattern
// AUTHOR : Manfred Duchrow
// VERSION : 1.7 - 13/02/2003
// HISTORY :
// 24/01/2000 duma CREATED
// 08/01/2002 duma bugfix -> Handle *xxx (equal characters after star) correctly
// 16/01/2002 duma changed -> Implements Serializable
// 06/07/2002 duma bugfix -> Couldn't match "London" on "L*n"
// 19/09/2002 duma bugfix -> Couldn't match "MA_DR_HRBLUB" on "*_HR*"
// 19/09/2002 duma changed -> Using now StringExaminer instead of CharacterIterator
// 29/09/2002 duma changed -> Refactored: Using StringExaminer instead of StringScanner
// 26/12/2002 duma changed -> Comment of matches() was wrong / new hasWildcard()
// 13/02/2003 duma added -> setDigitWildcardChar()
//
// Copyright (c) 2000-2003, by Manfred Duchrow. All rights reserved.
// ===========================================================================
package org.pf.text;
// ===========================================================================
// IMPORTS
// ===========================================================================
import java.io.Serializable ;
/**
* This class provides services for checking strings against string-patterns.
* Currently it supports the wildcards<br>
* '*' for any number of any character and <br>
* '?' for any one character.
*
* The API is very simple:<br>
* <br>
* There are only the two class methods <i>match()</i> and <i>matchIgnoreCase()</i>.
* <br>
* Example:
* <br>
* StringPattern.match( 'Hello World", "H* W*" ) ; --> evaluates to true <br>
* StringPattern.matchIgnoreCase( 'StringPattern", "str???pat*" ) ; --> evaluates to true <br>
*
*
* @author Manfred Duchrow
* @version 1.7
*/
public class StringPattern implements Serializable
{
// =========================================================================
// CONSTANTS
// =========================================================================
protected final static String MULTI_WILDCARD = "*" ;
protected final static char MULTICHAR_WILDCARD = '*' ;
protected final static char SINGLECHAR_WILDCARD = '?' ;
// =========================================================================
// INSTANCE VARIABLES
// =========================================================================
private boolean ignoreCase = false ;
/**
* Returns whether or not the pattern matching ignores upper and lower case
*/
public boolean getIgnoreCase() { return ignoreCase ; }
/**
* Sets whether the pattern matching should ignore case or not
*/
public void setIgnoreCase( boolean newValue ) { ignoreCase = newValue ; }
private String pattern = null ;
/**
* Returns the pattern as string.
*/
public String getPattern() { return pattern ; }
/**
* Sets the pattern to a new value
*/
public void setPattern( String newValue ) { pattern = newValue ; }
// -------------------------------------------------------------------------
private Character digitWildcard = null ;
protected Character digitWildcard() { return digitWildcard ; }
protected void digitWildcard( Character newValue ) { digitWildcard = newValue ; }
// =========================================================================
// CLASS METHODS
// =========================================================================
/**
* Returns true, if the given probe string matches the given pattern. <br>
* The character comparison is done case sensitive.
*
* @param probe The string to check against the pattern.
* @param pattern The patter, that probably contains wildcards ( '*' or '?' )
*/
public static boolean match( String probe, String pattern )
{
StringPattern stringPattern = new StringPattern( pattern, false ) ;
return ( stringPattern.matches( probe ) ) ;
} // match()
// -------------------------------------------------------------------------
/**
* Returns true, if the given probe string matches the given pattern. <br>
* The character comparison is done ignoring upper/lower-case.
*
* @param probe The string to check against the pattern.
* @param pattern The patter, that probably contains wildcards ( '*' or '?' )
*/
public static boolean matchIgnoreCase( String probe, String pattern )
{
StringPattern stringPattern = new StringPattern( pattern, true ) ;
return ( stringPattern.matches( probe ) ) ;
} // matchIgnoreCase()
// -------------------------------------------------------------------------
// =========================================================================
// CONSTRUCTORS
// =========================================================================
/**
* Initializes the new instance with the string pattern and the selecteion,
* if case should be ignored when comparing characters.
*
* @param pattern The pattern to check against ( May contain '*' and '?' wildcards )
* @param ignoreCase Definition, if case sensitive character comparison or not.
*/
public StringPattern( String pattern, boolean ignoreCase )
{
this.setPattern( pattern ) ;
this.setIgnoreCase( ignoreCase ) ;
} // StringPattern()
// -------------------------------------------------------------------------
/**
* Initializes the new instance with the string pattern.
* The default is case sensitive checking.
*
* @param pattern The pattern to check against ( May contain '*' and '?' wildcards )
*/
public StringPattern( String pattern )
{
this( pattern, false) ;
} // StringPattern()
// -------------------------------------------------------------------------
/**
* Initializes the new instance with the string pattern and a digit wildcard
* character.
* The default is case sensitive checking.
*
* @param pattern The pattern to check against ( May contain '*', '?' wildcards and the digit wildcard )
* @param digitWildcard A wildcard character that stands as placeholder for digits
*/
public StringPattern( String pattern, char digitWildcard )
{
this( pattern, false, digitWildcard ) ;
} // StringPattern()
// -------------------------------------------------------------------------
/**
* Initializes the new instance with the string pattern and the selecteion,
* if case should be ignored when comparing characters plus a wildcard
* character for digits.
*
* @param pattern The pattern to check against ( May contain '*' and '?' wildcards )
* @param ignoreCase Definition, if case sensitive character comparison or not.
* @param digitWildcard A wildcard character that stands as placeholder for digits
*/
public StringPattern( String pattern, boolean ignoreCase, char digitWildcard )
{
this.setPattern( pattern ) ;
this.setIgnoreCase( ignoreCase ) ;
this.setDigitWildcardChar( digitWildcard ) ;
} // StringPattern()
// -------------------------------------------------------------------------
// =========================================================================
// PUBLIC INSTANCE METHODS
// =========================================================================
/**
* Tests if a specified string matches the pattern.
*
* @param probe The string to compare to the pattern
* @return true if and only if the probe matches the pattern, false otherwise.
*/
public boolean matches( String probe )
{
StringExaminer patternIterator = null ;
StringExaminer probeIterator = null ;
char patternCh = '-' ;
char probeCh = '-' ;
String newPattern = null ;
String subPattern = null ;
int charIndex = 0 ;
if ( probe == null ) return false ;
if ( probe.length() == 0 ) return false ;
patternIterator = this.newExaminer( this.getPattern() ) ;
probeIterator = this.newExaminer( probe ) ;
probeCh = probeIterator.nextChar() ;
patternCh = this.getPatternChar( patternIterator, probeCh ) ;
while ( ( this.endNotReached( patternCh ) ) &&
( this.endNotReached( probeCh ) ) )
{
if ( patternCh == MULTICHAR_WILDCARD )
{
patternCh = this.skipWildcards( patternIterator ) ;
if ( this.endReached( patternCh ) )
{
return true ; // No more characters after multi wildcard - So everything matches
}
else
{
patternIterator.skip(-1) ;
newPattern = this.upToEnd( patternIterator ) ;
charIndex = newPattern.indexOf( MULTICHAR_WILDCARD ) ;
if ( charIndex >= 0 )
{
subPattern = newPattern.substring( 0, charIndex ) ;
if ( this.skipAfter( probeIterator, subPattern ) )
{
patternIterator = this.newExaminer( newPattern.substring( charIndex ) ) ;
patternCh = probeCh ;
}
else
{
return false ;
}
}
else
{
probeIterator.skip(-1) ;
return this.matchReverse( newPattern, probeIterator ) ;
}
}
}
if ( this.charsAreEqual( probeCh, patternCh ) )
{
if ( this.endNotReached(patternCh) )
{
probeCh = probeIterator.nextChar() ;
patternCh = this.getPatternChar( patternIterator, probeCh ) ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -