Example usage for java.util.regex PatternSyntaxException getIndex

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

Introduction

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

Prototype

public int getIndex() 

Source Link

Document

Retrieves the error index.

Usage

From source file:Main.java

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

private static void initResources() {
    try {/* ww  w  .  j  ava  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:com.marvelution.hudson.plugins.apiv2.APIv2Plugin.java

/**
 * Web Method to validate a given {@link Pattern}
 * /*from   www  . jav a  2s. c om*/
 * @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:ch.cyberduck.ui.cocoa.controller.PreferencesController.java

private void mark(NSMutableAttributedString text, PatternSyntaxException e) {
    if (null == e) {
        text.removeAttributeInRange(NSAttributedString.ForegroundColorAttributeName,
                NSRange.NSMakeRange(new NSUInteger(0), text.length()));
        return;//ww w . j av  a  2  s .com
    }
    int index = e.getIndex(); //The approximate index in the pattern of the error
    NSRange range = null;
    if (-1 == index) {
        range = NSRange.NSMakeRange(new NSUInteger(0), text.length());
    }
    if (index < text.length().intValue()) {
        //Initializes the NSRange with the range elements of location and length;
        range = NSRange.NSMakeRange(new NSUInteger(index), new NSUInteger(1));
    }
    text.addAttributesInRange(RED_FONT, range);
}

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  w  ww  .  j  av a  2  s  .  com*/
        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.sourceforge.vulcan.web.struts.forms.ProjectConfigForm.java

private void validateRegex(ActionErrors errors, String regex, String propertyName) {
    if (isNotBlank(regex)) {
        try {//from   w  w  w.  java  2 s  .  c o m
            Pattern.compile(regex);
        } catch (PatternSyntaxException e) {
            errors.add(propertyName, new ActionMessage("errors.regex", e.getDescription(), e.getIndex()));
        }
    }

}

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

public String getLogComments(String filename) {
    Pattern p = null;//w  ww.  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();
}