001: package org.makumba.commons;
002:
003: import java.util.regex.Matcher;
004: import java.util.regex.Pattern;
005:
006: /**
007: * This class provides several constants which are parts of patterns, and some methods for testing.
008: *
009: * @author Rudolf Mayer
010: * @version $Id: RegExpUtils.java,v 1.1 Sep 15, 2007 3:44:49 AM rudi Exp $
011: */
012: public class RegExpUtils {
013: public static final String identifier = "\\w[\\w|\\d]*";
014:
015: public static final String whitespace = "[\\s]*";
016:
017: public static final String minOneWhitespace = whitespace + "+";
018:
019: public static final String minOneLineWhitespace = "[ \\t]+";
020:
021: public static final String LineWhitespaces = "[ \\t]*";
022:
023: public static final String digit = "\\d";
024:
025: public static final String minOneDigit = digit + "+";
026:
027: public static final String nonDigit = "\\D";
028:
029: public static final String nonWhitespace = "[^\\s]";
030:
031: public static final String nonWhitespaces = nonWhitespace + "+";
032:
033: public static final String minOneNonWhitespace = "[^\\s]+";
034:
035: public static final String word = "\\w";
036:
037: public static final String minOneWord = word + "+";
038:
039: public static final String nonWord = "\\W";
040:
041: public static final String fieldName = "[a-zA-Z]" + word + "*"
042: + "(?:\\.\\w+)?";
043:
044: public static final String fieldNameAndSpaces = fieldName
045: + LineWhitespaces;
046:
047: public static String or(String[] options) {
048: String s = "(";
049: for (int i = 0; i < options.length; i++) {
050: s += options[i];
051: if (i + 1 < options.length) {
052: s += "|";
053: }
054: }
055: return s + ")";
056: }
057:
058: public static void main(String[] args) {
059: System.out.println(or(new String[] { "a", "b", "c" }));
060: testIdentifiers();
061: }
062:
063: /** testing method. */
064: public static void testIdentifiers() {
065: for (int i = 0; i < 256; i++) {
066: char ch = (char) i;
067: if (Character.isJavaIdentifierPart(ch)) {
068: System.out.print(i + ":\t");
069: if (Character.isJavaIdentifierStart(ch)) {
070: System.out.print(ch);
071: }
072: System.out.println("\t" + ch);
073: }
074: }
075: }
076:
077: /** Testing method to see if some Strings match a pattern. */
078: public static void evaluate(Pattern p, String[] rules,
079: boolean details) {
080: System.out.println(p.pattern());
081: for (int i = 0; i < rules.length; i++) {
082: Matcher matcher = p.matcher(rules[i].trim());
083: System.out.println(rules[i].trim() + ":"
084: + matcher.matches());
085: if (matcher.matches() && details) {
086: System.out.print("groups:" + matcher.groupCount());
087: for (int j = 0; j < matcher.groupCount(); j++) {
088: System.out
089: .print("\t|" + matcher.group(j + 1) + "|");
090: }
091: System.out.println();
092: }
093: }
094: }
095:
096: public static void evaluate(Pattern p, String[] rules) {
097: evaluate(p, rules, true);
098: }
099:
100: }
|