Example usage for java.util.regex PatternSyntaxException printStackTrace

List of usage examples for java.util.regex PatternSyntaxException printStackTrace

Introduction

In this page you can find the example usage for java.util.regex PatternSyntaxException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:ValidationTestWithPatternAndMatcher.java

public static void main(String args[]) {
    Pattern p = null;//  w  w  w  .  ja v  a 2  s. c o m
    try {
        p = Pattern.compile("Java \\d");
    } catch (PatternSyntaxException pex) {
        pex.printStackTrace();
        System.exit(0);
    }

    String candidate = "I love Java 5";
    Matcher m = p.matcher(candidate);

    System.out.println("result=" + m.find());
}

From source file:MainClass.java

public static void main(String args[]) {

    String phrase = "a word word";
    String duplicatePattern = "\\b(\\w+) \\1\\b";

    Pattern p = null;// ww  w .  j av  a  2s  .  com
    try {
        p = Pattern.compile(duplicatePattern);
    } catch (PatternSyntaxException pex) {
        pex.printStackTrace();
        System.exit(0);
    }
    // count the number of matches.
    int matches = 0;

    // get the matcher
    Matcher m = p.matcher(phrase);
    String val = null;

    // find all matching Strings
    while (m.find()) {
        val = ":" + m.group() + ":";
        System.out.println(val);
        matches++;
    }
}

From source file:MatchDuplicateWords.java

public static boolean hasDuplicate(String phrase) {
    boolean retval = false;
    String duplicatePattern = "\\b(\\w+) \\1\\b";
    Pattern p = null;//from w w  w  .  j  av  a2s.  com
    try {
        p = Pattern.compile(duplicatePattern);
    } catch (PatternSyntaxException pex) {
        pex.printStackTrace();
        System.exit(0);
    }
    int matches = 0;
    Matcher m = p.matcher(phrase);
    String val = null;

    while (m.find()) {
        retval = true;
        val = ":" + m.group() + ":";
        System.out.println(val);
        matches++;
    }

    String msg = "   NO MATCH: pattern:" + phrase + "\r\n             regex: " + duplicatePattern;

    if (retval) {
        msg = " MATCH     : pattern:" + phrase + "\r\n         regex: " + duplicatePattern;
    }

    System.out.println(msg + "\r\n");
    return retval;
}

From source file:Main.java

/**
 * Creates a regular expression pattern that matches a "wildcard" pattern.
 * /*  w  w  w . j  a va  2  s. com*/
 * @param wildcard The wildcard pattern.
 * @param matchCase Whether the pattern should be case sensitive.
 * @param escapeStartChar Whether to escape a starting <code>'^'</code>
 *        character.
 * @return The pattern.
 */
public static Pattern wildcardToPattern(String wildcard, boolean matchCase, boolean escapeStartChar) {

    int flags = 0;
    if (!matchCase) {
        flags = Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
    }

    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < wildcard.length(); i++) {
        char ch = wildcard.charAt(i);
        switch (ch) {
        case '*':
            sb.append(".*");
            break;
        case '?':
            sb.append('.');
            break;
        case '^':
            if (i > 0 || escapeStartChar) {
                sb.append('\\');
            }
            sb.append('^');
            break;
        case '\\':
        case '.':
        case '|':
        case '+':
        case '-':
        case '$':
        case '[':
        case ']':
        case '{':
        case '}':
        case '(':
        case ')':
            sb.append('\\').append(ch);
            break;
        default:
            sb.append(ch);
            break;
        }
    }

    Pattern p = null;
    try {
        p = Pattern.compile(sb.toString(), flags);
    } catch (PatternSyntaxException pse) {
        pse.printStackTrace();
        p = Pattern.compile(".+");
    }

    return p;

}

From source file:org.xchain.framework.util.RegExUtil.java

/**
 * Compiles a regular expression and logs an problems to the provided log.  If
 * a compile error occures, then null is returned.  This class will not rethrow the
 * error, so that it's use in a static context will not cause a ClassNotFoundException.
 *//*from  w w w.j  a v a2 s  . c o m*/
public static Pattern compilePattern(String regex, Logger log, String errorMessage) {
    Pattern pattern = null;
    try {
        // Compile the pattern.
        pattern = Pattern.compile(regex);
    } catch (PatternSyntaxException pse) {
        if (log.isErrorEnabled()) {
            log.error(errorMessage, pse);
        }
        pse.printStackTrace();
    }
    return pattern;
}

From source file:org.xchain.framework.util.RegExUtil.java

/**
 * Exists to allow an upgrade path from Commons Logging to SLF4J
 * //from w w  w .j a  va  2 s .c  o m
 * @param regex
 * @param log
 * @param errorMessage
 * @return
 */
@Deprecated
public static Pattern compilePattern(String regex, org.apache.commons.logging.Log log, String errorMessage) {
    Pattern pattern = null;
    try {
        // Compile the pattern.
        pattern = Pattern.compile(regex);
    } catch (PatternSyntaxException pse) {
        if (log.isErrorEnabled()) {
            log.error(errorMessage, pse);
        }
        pse.printStackTrace();
    }
    return pattern;
}

From source file:org.loklak.susi.SusiInference.java

/**
 * "see" defines a new thought based on the names given in the "transferExpr" and retrieved using the content of
 * a variable in the "expr" expression using a matching in the given pattern. It can be used to check if something
 * learned in the flow matches against a known pattern. When the matching is successful, that defines a new knowledge
 * pieces that are stored in the resulting thought thus extending an argument with new insights.
 * The "transferExpr" must be constructed using variables of the name schema "%1%", "%2%", .. which contain matches
 * of the variables from the expr retrieval in the flow with the pattern.
 * @param flow the argument flow// w w  w . j  ava 2 s  . c  o  m
 * @param transferExpr SQL-like transfer expression, like "a AS akk, b AS bit". These defined variables stored in the flow as next thought
 * @param expr the name of a variable entity in the argument flow. The content of that variable is matched in the pattern
 * @param pattern the
 * @return a new thought containing variables from the matcher in the pattern
 */
private static final SusiThought see(SusiArgument flow, String transferExpr, String expr, Pattern pattern) {
    // example: see $1$ as idea from ""
    SusiThought nextThought = new SusiThought();
    try {
        Matcher m = pattern.matcher(flow.unify(expr, 0));
        int gc;
        if (m.matches() && (gc = m.groupCount()) > 0) {
            SusiTransfer transfer = new SusiTransfer(transferExpr);
            JSONObject choice = new JSONObject();
            for (int i = 0; i < gc; i++)
                choice.put("%" + (i + 1) + "%", m.group(i));
            JSONObject seeing = transfer.extract(choice);
            for (String key : seeing.keySet()) {
                String observed = seeing.getString(key);
                nextThought.addObservation(key, observed);
            }
        }
    } catch (PatternSyntaxException e) {
        e.printStackTrace();
    }
    return nextThought; // an empty thought is a fail signal
}

From source file:ai.susi.mind.SusiInference.java

/**
 * "see" defines a new thought based on the names given in the "transferExpr" and retrieved using the content of
 * a variable in the "expr" expression using a matching in the given pattern. It can be used to check if something
 * learned in the flow matches against a known pattern. When the matching is successful, that defines a new knowledge
 * pieces that are stored in the resulting thought thus extending an argument with new insights.
 * The "transferExpr" must be constructed using variables of the name schema "%1%", "%2%", .. which contain matches
 * of the variables from the expr retrieval in the flow with the pattern.
 * @param flow the argument flow/*from   w  w  w.ja va  2  s .c  o  m*/
 * @param transferExpr SQL-like transfer expression, like "a AS akk, b AS bit". These defined variables stored in the flow as next thought
 * @param expr the name of a variable entity in the argument flow. The content of that variable is matched in the pattern
 * @param pattern the
 * @return a new thought containing variables from the matcher in the pattern
 */
private static final SusiThought see(SusiArgument flow, String transferExpr, String expr, Pattern pattern) {
    // example: see $1$ as idea from ""
    SusiThought nextThought = new SusiThought();
    try {
        Matcher m = pattern.matcher(flow.unify(expr, 0));
        int gc = -1;
        if (new TimeoutMatcher(m).matches()) {
            SusiTransfer transfer = new SusiTransfer(transferExpr);
            JSONObject choice = new JSONObject();
            if ((gc = m.groupCount()) > 0) {
                for (int i = 0; i < gc; i++)
                    choice.put("%" + (i + 1) + "%", m.group(i));
            } else {
                choice.put("%1%", expr);
            }
            JSONObject seeing = transfer.extract(choice);
            for (String key : seeing.keySet()) {
                String observed = seeing.getString(key);
                nextThought.addObservation(key, observed);
            }
        }
    } catch (PatternSyntaxException e) {
        e.printStackTrace();
    }
    return nextThought; // an empty thought is a fail signal
}

From source file:org.paxle.filter.blacklist.impl.Blacklist.java

/**
 * @see org.paxle.filter.blacklist.IBlacklist#editPattern(String, String)
 *///ww w.j a  v a 2s.  co m
public boolean editPattern(String fromPattern, String toPattern) {
    try {
        Pattern.compile(toPattern);
        return (this.removePattern(fromPattern) && this.addPattern(toPattern));
    } catch (PatternSyntaxException e) {
        e.printStackTrace();
        return false;
    }

}

From source file:org.sonews.daemon.command.XPatCommand.java

@Override
public void processLine(NNTPConnection conn, final String line, byte[] raw)
        throws IOException, StorageBackendException {
    if (conn.getCurrentGroup() == null) {
        conn.println("430 no group selected");
        return;/* w  ww.  j av  a2  s.c om*/
    }

    String[] command = line.split("\\p{Space}+");

    // There may be multiple patterns and Thunderbird produces
    // additional spaces between range and pattern
    if (command.length >= 4) {
        String header = command[1].toLowerCase(Locale.US);
        String range = command[2];
        String pattern = command[3];

        long start = -1;
        long end = -1;
        if (range.contains("-")) {
            String[] rsplit = range.split("-", 2);
            start = Long.parseLong(rsplit[0]);
            if (rsplit[1].length() > 0) {
                end = Long.parseLong(rsplit[1]);
            }
        } else // TODO: Handle Message-IDs
        {
            start = Long.parseLong(range);
        }

        try {
            List<Pair<Long, String>> heads = StorageManager.current().getArticleHeaders(conn.getCurrentGroup(),
                    start, end, header, pattern);

            conn.println("221 header follows");
            for (Pair<Long, String> head : heads) {
                conn.println(head.getA() + " " + head.getB());
            }
            conn.println(".");
        } catch (PatternSyntaxException ex) {
            ex.printStackTrace();
            conn.println("500 invalid pattern syntax");
        } catch (StorageBackendException ex) {
            ex.printStackTrace();
            conn.println("500 internal server error");
        }
    } else {
        conn.println("430 invalid command usage");
    }
}