Example usage for java.util.regex PatternSyntaxException getPattern

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

Introduction

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

Prototype

public String getPattern() 

Source Link

Document

Retrieves the erroneous regular-expression pattern.

Usage

From source file:Main.java

public static void main(String[] args) {
    Pattern pattern = null;//from   w w  w.j av  a 2  s.  c om
    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:RegexTestHarness2.java

private static void initResources() {
    try {/*from  w  w  w .j av  a  2 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:Correct.java

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

    Two = Two.replaceAll("\\\\n", "\n");
    try {// w w w . ja  va  2s .  c  om
        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:net.triptech.metahive.model.Record.java

/**
 * Apply the regular expression to the supplied record.
 *
 * @param record the record//from   www  .ja  v a2 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:com.marvelution.hudson.plugins.apiv2.APIv2Plugin.java

/**
 * Web Method to validate a given {@link Pattern}
 * /*from   ww  w  .j ava 2 s . c  o m*/
 * @param value the {@link Pattern} to validate
 * @return validation result
 * @throws IOException in case of errors
 * @throws ServletException in case of errors
 */
public FormValidation doCheckPattern(@QueryParameter final String value) throws IOException, ServletException {
    if (!Hudson.getInstance().hasPermission(Hudson.ADMINISTER) || StringUtils.isBlank(value)) {
        return FormValidation.ok();
    }
    try {
        Pattern.compile(value);
        return FormValidation.ok();
    } catch (PatternSyntaxException e) {
        StringBuilder builder = new StringBuilder(e.getDescription());
        if (e.getIndex() >= 0) {
            builder.append(" near index ").append(e.getIndex());
        }
        builder.append("<pre>");
        builder.append(e.getPattern()).append(System.getProperty("line.separator"));
        if (e.getIndex() >= 0) {
            for (int i = 0; i < e.getIndex(); ++i) {
                builder.append(' ');
            }
            builder.append('^');
        }
        builder.append("</pre>");
        return FormValidation.errorWithMarkup(builder.toString());
    }
}

From source file:net.sourceforge.vulcan.core.support.AbstractProjectDomBuilder.java

private void linkifyCommitMessage(Element changeSet, ChangeSetDto changes, String projectName) {
    final CommitLogParser commitLogParser = new CommitLogParser();

    try {/*from  www  . ja  v  a 2s .  c  om*/
        final ProjectConfigDto projectConfig = projectManager.getProjectConfig(projectName);

        commitLogParser.setKeywordPattern(projectConfig.getBugtraqLogRegex1());
        commitLogParser.setIdPattern(projectConfig.getBugtraqLogRegex2());
    } catch (NoSuchProjectException ignore) {
    }

    try {
        commitLogParser.parse(changes.getMessage());

        changeSet.addContent(commitLogParser.getMessageNode());
    } catch (PatternSyntaxException e) {
        eventHandler.reportEvent(new ErrorEvent(this, "errors.bugtraq.regex",
                new Object[] { e.getPattern(), e.getDescription(), e.getIndex() }, e));

        final Element message = new Element("message");
        message.setText(changes.getMessage());
        changeSet.addContent(message);
    }
}

From source file:net.triptech.metahive.CalculationParser.java

/**
 * Builds the calculation./*  www  .jav  a 2s  . c om*/
 *
 * @param calculation the calculation
 * @param values the values
 * @return the string
 */
public static String buildCalculation(String calculation, Map<Long, Double> values) {

    String parsedCalculation = "";

    logger.debug("Calculation: " + calculation);
    logger.debug("Values: " + values);

    if (StringUtils.isNotBlank(calculation) && values != null) {
        try {
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(calculation);
            StringBuffer sb = new StringBuffer();
            logger.debug("Regular expression: " + regEx);
            while (m.find()) {
                logger.info("Variable instance found: " + m.group());
                try {
                    String text = m.group();
                    Long id = Long.parseLong(StringUtils.substring(text, 1));
                    logger.info("Variable id: " + id);

                    if (values.containsKey(id)) {
                        logger.debug("Contains variable " + id);
                        double value = values.get(id);
                        logger.debug("Value: " + value);
                        text = String.valueOf(value);
                    }
                    logger.debug("Replacement text: " + text);
                    m.appendReplacement(sb, Matcher.quoteReplacement(text));

                } catch (NumberFormatException nfe) {
                    logger.error("Error parsing variable id");
                }
            }
            m.appendTail(sb);

            parsedCalculation = sb.toString();
            logger.info("Parsed calculation: " + parsedCalculation);

        } catch (PatternSyntaxException pe) {
            logger.error("Regex syntax error ('" + pe.getPattern() + "') " + pe.getMessage());
        }
    }
    return parsedCalculation;
}

From source file:net.triptech.metahive.CalculationParser.java

/**
 * Marks up the calculation./*from  www  . ja v a  2 s.c o  m*/
 *
 * @param calculation the calculation
 * @return the string
 */
public static String maredUpCalculation(String calculation) {

    String markedUpCalculation = "";

    logger.debug("Calculation: " + calculation);

    if (StringUtils.isNotBlank(calculation)) {
        try {
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(calculation);
            StringBuffer sb = new StringBuffer();
            logger.debug("Regular expression: " + regEx);
            while (m.find()) {
                logger.info("Variable instance found: " + m.group());
                try {
                    String text = "<span class=\"variable\">" + m.group().toUpperCase() + "</span>";

                    m.appendReplacement(sb, Matcher.quoteReplacement(text));

                } catch (NumberFormatException nfe) {
                    logger.error("Error parsing variable id");
                }
            }
            m.appendTail(sb);

            markedUpCalculation = sb.toString();
            logger.info("Marked up calculation: " + markedUpCalculation);

        } catch (PatternSyntaxException pe) {
            logger.error("Regex syntax error ('" + pe.getPattern() + "') " + pe.getMessage());
        }
    }
    return markedUpCalculation;
}

From source file:net.triptech.metahive.CalculationParser.java

/**
 * Parses the calculation to identify what variables are referenced in it.
 *
 * @param calculation the calculation//from  www  .jav a  2 s.co m
 * @return the list
 */
public static Set<Long> parseVariableIds(final String calculation) {

    Set<Long> variableIds = new TreeSet<Long>();

    logger.debug("Calculation: " + calculation);

    if (StringUtils.isNotBlank(calculation)) {
        try {
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(calculation);
            logger.debug("Regular expression: " + regEx);
            while (m.find()) {
                logger.info("Variable instance found: " + m.group());
                try {
                    Long id = Long.parseLong(StringUtils.substring(m.group(), 1));
                    logger.debug("Variable id: " + id);
                    variableIds.add(id);
                } catch (NumberFormatException nfe) {
                    logger.error("Error parsing variable id");
                }
            }
        } catch (PatternSyntaxException pe) {
            logger.error("Regex syntax error ('" + pe.getPattern() + "') " + pe.getMessage());
        }
    }

    logger.info(variableIds.size() + " ids found");

    return variableIds;
}

From source file:sorcer.core.provider.logger.RemoteLoggerManager.java

public String getLogComments(String filename) {
    Pattern p = null;/*from   w w  w  .j a v  a  2 s . c  o  m*/
    try {
        // The following pattern lets this extract multiline comments that
        // appear on a single line (e.g., /* same line */) and single-line
        // comments (e.g., // some line). Furthermore, the comment may
        // appear anywhere on the line.
        p = Pattern.compile(".*/\\*.*\\*/|.*//.*$");
    } catch (PatternSyntaxException e) {
        System.err.println("Regex syntax error: " + e.getMessage());
        System.err.println("Error description: " + e.getDescription());
        System.err.println("Error index: " + e.getIndex());
        System.err.println("Erroneous pattern: " + e.getPattern());
    }
    BufferedReader br = null;
    StringBuffer bw = new StringBuffer();
    try {
        FileReader fr = new FileReader(filename);
        br = new BufferedReader(fr);
        Matcher m = p.matcher("");
        String line;
        while ((line = br.readLine()) != null) {
            m.reset(line);
            if (m.matches()) /* entire line must match */
            {
                bw.append(line + "\n");
            }
        }
    } catch (IOException e) {
        System.err.println(e.getMessage());
    } finally // Close file.
    {
        try {
            if (br != null)
                br.close();
        } catch (IOException e) {
        }
    }
    return bw.toString();
}