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()));
}
}
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()));
}
}
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();
}
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"));
}
}
}
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);
}
}
Aggregations