Search in sources :

Example 66 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project ballerina by ballerina-lang.

the class ReplaceAllWithRegex method execute.

@Override
public void execute(Context context) {
    String mainString = context.getStringArgument(0);
    String replaceWith = context.getStringArgument(1);
    BStruct regexStruct = (BStruct) context.getRefArgument(0);
    try {
        Pattern pattern = validatePattern(regexStruct);
        Matcher matcher = pattern.matcher(mainString);
        String replacedString = matcher.replaceAll(replaceWith);
        context.setReturnValues(new BString(replacedString));
    } catch (PatternSyntaxException e) {
        context.setReturnValues(BLangVMErrors.createError(context, 0, e.getMessage()));
    }
}
Also used : Pattern(java.util.regex.Pattern) BStruct(org.ballerinalang.model.values.BStruct) Matcher(java.util.regex.Matcher) BString(org.ballerinalang.model.values.BString) BString(org.ballerinalang.model.values.BString) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 67 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project ballerina by ballerina-lang.

the class ReplaceFirstWithRegex method execute.

@Override
public void execute(Context context) {
    String mainString = context.getStringArgument(0);
    String replaceWith = context.getStringArgument(1);
    BStruct regexStruct = (BStruct) context.getRefArgument(0);
    try {
        Pattern pattern = validatePattern(regexStruct);
        Matcher matcher = pattern.matcher(mainString);
        String replacedString = matcher.replaceFirst(replaceWith);
        context.setReturnValues(new BString(replacedString));
    } catch (PatternSyntaxException e) {
        context.setReturnValues(BLangVMErrors.createError(context, 0, e.getMessage()));
    }
}
Also used : Pattern(java.util.regex.Pattern) BStruct(org.ballerinalang.model.values.BStruct) Matcher(java.util.regex.Matcher) BString(org.ballerinalang.model.values.BString) BString(org.ballerinalang.model.values.BString) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 68 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project eclipse-cs by checkstyle.

the class FileMatchPatternEditDialog method okPressed.

/**
 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
 */
@Override
protected void okPressed() {
    String pattern = mFileMatchPatternText.getText();
    try {
        // 
        // Try compiling the pattern using the regular expression compiler.
        // 
        Pattern.compile(pattern);
        if (mPattern == null) {
            mPattern = new FileMatchPattern(pattern);
        } else {
            mPattern.setMatchPattern(pattern);
        }
        mPattern.setIsIncludePattern(mIncludeButton.getSelection());
    } catch (PatternSyntaxException e) {
        this.setErrorMessage(e.getLocalizedMessage());
        return;
    } catch (CheckstylePluginException e) {
        this.setErrorMessage(e.getLocalizedMessage());
        return;
    }
    super.okPressed();
}
Also used : FileMatchPattern(net.sf.eclipsecs.core.projectconfig.FileMatchPattern) CheckstylePluginException(net.sf.eclipsecs.core.util.CheckstylePluginException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 69 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project UniversalMediaServer by UniversalMediaServer.

the class TracesTab method searchTraces.

private void searchTraces() {
    boolean found = false;
    Matcher match = null;
    Document document = jList.getDocument();
    int flags = Pattern.UNICODE_CASE;
    String find = jSearchBox.getText();
    if (find.isEmpty()) {
        jSearchOutput.setText("");
    } else if (document.getLength() > 0) {
        if (jMLSearch.isSelected()) {
            flags += Pattern.DOTALL + Pattern.MULTILINE;
        }
        if (!jRESearch.isSelected()) {
            flags += Pattern.LITERAL;
        }
        if (!jCSSearch.isSelected()) {
            flags += Pattern.CASE_INSENSITIVE;
        }
        try {
            if (searchPattern == null || !find.equals(searchPattern.pattern()) || flags != searchPattern.flags()) {
                searchPattern = Pattern.compile(find, flags);
            }
            match = searchPattern.matcher(document.getText(0, document.getLength()));
            found = match.find(jList.getCaretPosition());
            if (!found && match.hitEnd()) {
                found = match.find(0);
            }
            if (found) {
                jList.requestFocusInWindow();
                Rectangle viewRect = jList.modelToView(match.start());
                Rectangle viewRectEnd = jList.modelToView(match.end());
                if (viewRectEnd.x < viewRect.x) {
                    viewRectEnd.x = jList.getWidth();
                }
                viewRect.width = viewRectEnd.x - viewRect.x;
                viewRect.height += viewRectEnd.y - viewRect.y;
                jList.scrollRectToVisible(viewRect);
                jList.setCaretPosition(match.start());
                jList.moveCaretPosition(match.end());
                jSearchOutput.setText("");
            } else {
                jSearchOutput.setText(String.format(Messages.getString("TracesTab.21"), jSearchBox.getText()));
            }
        } catch (PatternSyntaxException pe) {
            jSearchOutput.setText(String.format(Messages.getString("TracesTab.22"), pe.getLocalizedMessage()));
        } catch (Exception ex) {
            LOGGER.debug("Exception caught while searching traces list: " + ex);
            jSearchOutput.setText(Messages.getString("TracesTab.23"));
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) Rectangle(java.awt.Rectangle) Document(javax.swing.text.Document) PatternSyntaxException(java.util.regex.PatternSyntaxException) IOException(java.io.IOException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 70 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project ASCIIGenome by dariober.

the class TrackSet method setTrackHeightForRegex.

/**
 * From cmdInput extract regex and yMaxLines then iterate through the tracks list to set
 * the yMaxLines in the tracks whose filename matches the regex.
 * The input list is updated in place!
 * @throws SQLException
 * @throws InvalidRecordException
 * @throws InvalidGenomicCoordsException
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws MalformedURLException
 */
public void setTrackHeightForRegex(List<String> tokens) throws InvalidCommandLineException, MalformedURLException, ClassNotFoundException, IOException, InvalidGenomicCoordsException, InvalidRecordException, SQLException {
    // MEMO of subcommand syntax:
    // 0 trackHeight
    // 1 int    mandatory
    // 2 regex  optional
    boolean invertSelection = Utils.argListContainsFlag(tokens, "-v");
    if (tokens.size() < 2) {
        System.err.println("Error in trackHeight subcommand. Expected 2 args got: " + tokens);
        throw new InvalidCommandLineException();
    }
    // Get height
    try {
        this.trackHeightForRegex = Integer.parseInt(tokens.get(1));
        this.trackHeightForRegex = this.trackHeightForRegex < 0 ? 0 : this.trackHeightForRegex;
    } catch (NumberFormatException e) {
        System.err.println("Number format exception: " + this.trackHeightForRegex);
        throw new InvalidCommandLineException();
    }
    // Regex
    List<String> trackNameRegex = new ArrayList<String>();
    if (tokens.size() >= 3) {
        trackNameRegex = tokens.subList(2, tokens.size());
    } else {
        // Default: Capture everything
        trackNameRegex.add(".*");
    }
    // Update
    this.regexForTrackHeight.clear();
    for (String x : trackNameRegex) {
        try {
            this.regexForTrackHeight.add(Pattern.compile(x));
        } catch (PatternSyntaxException e) {
            System.err.println("Command: " + tokens);
            System.err.println("Invalid regex in: " + x);
            System.err.println(e.getDescription());
            throw new InvalidCommandLineException();
        }
    }
    // And set as required:
    List<Track> tracksToReset = this.matchTracks(trackNameRegex, true, invertSelection);
    for (Track tr : tracksToReset) {
        tr.setyMaxLines(this.trackHeightForRegex);
    }
}
Also used : InvalidCommandLineException(exceptions.InvalidCommandLineException) ArrayList(java.util.ArrayList) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

PatternSyntaxException (java.util.regex.PatternSyntaxException)355 Pattern (java.util.regex.Pattern)190 Matcher (java.util.regex.Matcher)115 ArrayList (java.util.ArrayList)46 IOException (java.io.IOException)25 List (java.util.List)19 File (java.io.File)14 Map (java.util.Map)12 HashMap (java.util.HashMap)9 URL (java.net.URL)7 HashSet (java.util.HashSet)7 Iterator (java.util.Iterator)7 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)7 BufferedReader (java.io.BufferedReader)6 Collection (java.util.Collection)6 LinkedList (java.util.LinkedList)5 Test (org.junit.Test)5 InputStreamReader (java.io.InputStreamReader)4 SQLException (java.sql.SQLException)4 LinkedHashMap (java.util.LinkedHashMap)4