package com.mindprod.example;
import java.util.regex.Pattern;
import static java.lang.System.*;
/**
* Demonstrate techique of coding Regex patterns with fragments.
* <p/>
* Show some regexes coded with named fragments and alse the traditional way spelled out longhand.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2009-04-28
* @since 2009-04-28
*/
@SuppressWarnings( { "UnusedAssignment" } )
public class TestRegexFragments
{
private static final String A_DASH = "[/\\-]";
private static final String A_DD = "[0-3]\\d";
/**
* Regex to look for Windows EOL sequence
*/
private static final String A_EOL = "\\r\\n";
/**
* regex fragment to match a =
*/
private static final String A_EQUAL = "=";
private static final String A_MM = "\\d{2}";
/**
* regex to find a number eg. 10
*/
private static final String A_NUMBER = "\\d++";
/**
* regex fragment to match a "
*/
private static final String A_QUOTE = "\"";
/**
* regex fragment to look for some spaces
*/
private static final String A_SPACES = "\\s++";
/**
* regex fragment to look for <!--
*/
private static final String A_START_HTML_COMMENT = "<!\\-\\-";
/**
* regex to find a version number eg. 1.5 for program or JDK versions
*/
private static final String A_VERSION = "\\d++\\.\\d";
/**
* regex to find a word
*/
private static final String A_WORD = "\\w++]";
private static final String A_YYYY = "[12][90]\\d{2}";
private static final String A_YYYY_MM__DD = A_YYYY + A_DASH + A_MM + A_DASH + A_DD;
private static final String A_YYYY_MM__DD_PARTIAL = A_YYYY + "(?:" + A_DASH + A_MM + "(?:" + A_DASH + A_DD + ")?)?";
/**
* pattern to look for @version 9.9
*/
private static final Pattern FIND_AT_VERSION;
/**
* pattern to look for @version 9.9, done without fragments
*/
private static final Pattern FIND_AT_VERSION_OLD =
Pattern.compile( "@version\\s++(\\d++\\.\\d)" );
/**
* pattern to look for <!-- macro CMPApplet 1.6
*/
private static final Pattern FIND_JDK_REQUIRED_CMP_APPLET;
/**
* pattern to look for <!-- macro CMPApplet 1.6, done without fragments
*/
private static final Pattern FIND_JDK_REQUIRED_CMP_APPLET_OLD =
Pattern.compile( "<!\\-\\-\\s++macro\\s++CMPApplet\\s++(\\d++\\.\\d)\\s++" );
/**
* pattern to look for RELEASE_DATE = "2007-04-11", does not capture YYYY-MM-DD
*/
private static final Pattern FIND_RELEASE_DATE;
/**
* pattern to look for RELEASE_DATE = "2007-04-11", does not capture YYYY-MM-DD, done without fragments
*/
private static final Pattern FIND_RELEASE_DATE_OLD =
Pattern.compile( "RELEASE_DATE\\s++=\\s++\"[12][90]\\d{2}[/\\-]\\d{2}[/\\-][0-3]\\d\"" );
static
{
FIND_AT_VERSION = Pattern.compile( "@version"
+ A_SPACES
+ "("
+ A_VERSION
+ ")" );
FIND_JDK_REQUIRED_CMP_APPLET =
Pattern.compile( A_START_HTML_COMMENT
+ A_SPACES
+ "macro"
+ A_SPACES
+ "CMPApplet"
+ A_SPACES
+ "("
+ A_VERSION
+ ")"
+ A_SPACES );
FIND_RELEASE_DATE = Pattern.compile( "RELEASE_DATE"
+ A_SPACES
+ A_EQUAL
+ A_SPACES
+ A_QUOTE
+ A_YYYY_MM__DD
+ A_QUOTE );
}
/**
* main method. Reassure that both forms of Regex are equivalent.
*
* @param args not used
*/
public static void main( String[] args )
{
out.println( FIND_AT_VERSION.toString() );
assert FIND_AT_VERSION.toString().equals( FIND_AT_VERSION_OLD.toString() ) : "oops";
assert FIND_JDK_REQUIRED_CMP_APPLET.toString().equals( FIND_JDK_REQUIRED_CMP_APPLET_OLD.toString() ) : "oops";
assert FIND_RELEASE_DATE.toString().equals( FIND_RELEASE_DATE_OLD.toString() ) : "oops";
}
}