Example usage for java.util.regex PatternSyntaxException getMessage

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

Introduction

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

Prototype

public String getMessage() 

Source Link

Document

Returns a multi-line string containing the description of the syntax error and its index, the erroneous regular-expression pattern, and a visual indication of the error index within the pattern.

Usage

From source file:Main.java

public static void main(String[] args) {
    Pattern pattern = null;// w  w  w.  j  ava 2  s  .c  o  m
    Matcher matcher = null;

    Console console = System.console();
    while (true) {
        try {
            pattern = Pattern.compile(console.readLine("%nEnter your regex: "));

            matcher = pattern.matcher(console.readLine("Enter input string to search: "));
        } catch (PatternSyntaxException pse) {
            console.format("There is a problem with the regular expression!%n");
            console.format("The pattern in question is: %s%n", pse.getPattern());
            console.format("The description is: %s%n", pse.getDescription());
            console.format("The message is: %s%n", pse.getMessage());
            console.format("The index is: %s%n", pse.getIndex());
            System.exit(0);
        }
        boolean found = false;
        while (matcher.find()) {
            console.format("I found the text \"%s\" starting at " + "index %d and ending at index %d.%n",
                    matcher.group(), matcher.start(), matcher.end());
            found = true;
        }
        if (!found) {
            console.format("No match found.%n");
        }
    }
}

From source file:Correct.java

public static void execute(String One, String Two) {

    Two = Two.replaceAll("\\\\n", "\n");
    try {//from  w w  w. ja v a 2  s . com
        System.out.println("Regex = " + One);
        System.out.println("Input = " + Two);

        Pattern pattern = Pattern.compile(One);
        Matcher match = pattern.matcher(Two);

        while (match.find()) {
            System.out.println("Found [" + match.group() + "]\nStarting at " + match.start() + " , \nEnding at "
                    + (match.end() - 1));
        }
    } catch (PatternSyntaxException pse) {

        System.err.println("Bad regex: " + pse.getMessage());
        System.err.println("Description: " + pse.getDescription());
        System.err.println("Index: " + pse.getIndex());
        System.err.println("Incorrect pattern: " + pse.getPattern());
    }
}

From source file:GrepSun.java

private static void compile(String pat) {
    try {//from ww  w  . j av a 2 s  .  c om
        pattern = Pattern.compile(pat);
    } catch (PatternSyntaxException x) {
        System.err.println(x.getMessage());
        System.exit(1);
    }
}

From source file:Grep.java

/**
 * Compiles the pattern./*from   ww  w . j  a  v  a 2 s . co  m*/
 * 
 * @param pat
 *          regex
 */
private static void compile(String pat) {
    try {
        pattern = Pattern.compile(pat);
    } catch (PatternSyntaxException x) {
        System.err.println(x.getMessage());
    }
}

From source file:RegexTestHarness2.java

private static void initResources() {
    try {/* w ww  .j av  a2  s.  c  o m*/
        br = new BufferedReader(new FileReader("regex.txt"));
    } catch (FileNotFoundException fnfe) {
        System.out.println("Cannot locate input file! " + fnfe.getMessage());
        System.exit(0);
    }
    try {
        REGEX = br.readLine();
        INPUT = br.readLine();
    } catch (IOException ioe) {
    }

    try {
        pattern = Pattern.compile(REGEX);
        matcher = pattern.matcher(INPUT);
    } catch (PatternSyntaxException pse) {
        System.out.println("There is a problem with the regular expression!");
        System.out.println("The pattern in question is: " + pse.getPattern());
        System.out.println("The description is: " + pse.getDescription());
        System.out.println("The message is: " + pse.getMessage());
        System.out.println("The index is: " + pse.getIndex());
        System.exit(0);
    }

    System.out.println("Current REGEX is: " + REGEX);
    System.out.println("Current INPUT is: " + INPUT);
}

From source file:com.qwazr.crawler.web.manager.WebCrawlThread.java

private final static List<Matcher> getRegExpMatcherList(List<String> patternList) throws ServerException {
    if (patternList == null || patternList.isEmpty())
        return null;
    try {/*from   w  w w .j  av  a  2s  .  c  om*/
        List<Matcher> matcherList = new ArrayList<Matcher>(patternList.size());
        for (String pattern : patternList) {
            Matcher matcher = Pattern.compile(pattern).matcher(StringUtils.EMPTY);
            matcherList.add(matcher);
        }
        return matcherList;
    } catch (PatternSyntaxException e) {
        throw new ServerException(Status.NOT_ACCEPTABLE, e.getMessage());
    }
}

From source file:net.triptech.metahive.model.Record.java

/**
 * Apply the regular expression to the supplied record.
 *
 * @param record the record/*from   ww  w  .  j  av a  2  s  .co  m*/
 * @param regEx the reg ex
 * @return the string
 */
private static String regex(final String record, final String regEx) {

    String result = "";

    try {
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(record);
        if (m.find()) {
            result = m.group();
        }
    } catch (PatternSyntaxException pe) {
        logger.error("Regex syntax error ('" + pe.getPattern() + "') " + pe.getMessage());
    }
    return result;
}

From source file:net.firejack.platform.core.validation.MatchProcessor.java

@Override
public List<ValidationMessage> validate(Method readMethod, String property, Object value, ValidationMode mode)
        throws RuleValidationException {
    Match matchAnnotation = readMethod.getAnnotation(Match.class);
    if (matchAnnotation != null && StringUtils.isNotBlank(matchAnnotation.expression())) {
        Class<?> returnType = readMethod.getReturnType();
        if (returnType == String.class) {
            Pattern pattern = getCachedPatterns().get(matchAnnotation.expression());
            if (pattern == null) {
                try {
                    pattern = Pattern.compile(matchAnnotation.expression());
                    getCachedPatterns().put(matchAnnotation.expression(), pattern);
                } catch (PatternSyntaxException e) {
                    logger.error(e.getMessage(), e);
                    throw new ImproperValidationArgumentException(
                            "Pattern expression should have correct syntax.");
                }// w ww.j av  a 2  s . c  o m
            }
            List<ValidationMessage> messages = null;
            if (value != null) {
                String sValue = (String) value;
                if (StringUtils.isNotBlank(sValue) && !pattern.matcher(sValue).matches()) {
                    messages = new ArrayList<ValidationMessage>();
                    messages.add(new ValidationMessage(property, matchAnnotation.msgKey(),
                            matchAnnotation.parameterName()));
                }
            }
            return messages;
        }

    }
    return null;
}

From source file:com.thoughtworks.go.domain.DefaultCommentRenderer.java

public String render(String text) {
    if (StringUtils.isBlank(text)) {
        return "";
    }//from  w w w . j  a v a  2s . co m
    if (regex.isEmpty() || link.isEmpty()) {
        Comment comment = new Comment();
        comment.escapeAndAdd(text);
        return comment.render();
    }
    try {
        Matcher matcher = Pattern.compile(regex).matcher(text);
        int start = 0;
        Comment comment = new Comment();
        while (hasMatch(matcher)) {
            comment.escapeAndAdd(text.substring(start, matcher.start()));
            comment.add(dynamicLink(matcher));
            start = matcher.end();
        }
        comment.escapeAndAdd(text.substring(start));
        return comment.render();
    } catch (PatternSyntaxException e) {
        LOGGER.warn("Illegal regular expression: {} - {}", regex, e.getMessage());
    }
    return text;
}

From source file:io.wcm.devops.conga.plugins.aem.handlebars.helper.AbstractFilter.java

/**
 * Gets and removes value from map and validtes as regex.
 * @param map Map//from w w w .j a  v  a 2  s  .  co m
 * @param key Key
 * @return Value or null if not set
 */
protected final String getRegexValue(Map<String, Object> map, String key) {
    String value = getValue(map, key);

    // compile value to regex to validate it
    if (value != null) {
        try {
            Pattern.compile(value);
        } catch (PatternSyntaxException ex) {
            throw new IllegalArgumentException("Invalid regex for '" + key + "': " + ex.getMessage());
        }
    }

    return value;
}