public static BooleanValue regex( TextValue lhs, TextValue rhs ) { String regexString = rhs.stringValue(); try { boolean matches = Pattern.compile( regexString ).matcher( lhs.stringValue() ).matches(); return matches ? TRUE : FALSE; } catch ( PatternSyntaxException e ) { throw new InvalidSemanticsException( "Invalid Regex: " + e.getMessage() ); } }
/** * Returns null if the argument is a syntactically valid regular * expression with at least the given number of groups. Otherwise returns a * PatternSyntaxException describing why the argument is not a regex. * @param s string to check for being a regular expression * @param groups number of groups expected * @return null, or a PatternSyntaxException describing why the argument is not a regex. */ @SuppressWarnings("regex") // RegexUtil /*@SideEffectFree*/ public static /*@Nullable*/ PatternSyntaxException regexException(String s, int groups) { try { Pattern p = Pattern.compile(s); int actualGroups = getGroupCount(p); if (actualGroups < groups) { return new PatternSyntaxException(regexErrorMessage(s, groups, actualGroups), s, -1); } } catch (PatternSyntaxException pse) { return pse; } return null; }
boolean error = false; Matcher m = historyPush.matcher(message); if (m.matches()) { justPush = true; echo = true; message = m.group(1); m = editPattern.matcher(message); if (m.matches()) { String orig = m.group(1); String sub = m.group(2); try { Pattern pat = Pattern.compile(orig); Matcher subMatcher = pat.matcher(history.getLast(0)); if (subMatcher.find()) { putResponse("Bad regexp: " + pse.getDescription());
private RegexTesterResponse doTestRegex(String example, String regex) { final Pattern pattern; try { pattern = Pattern.compile(regex, Pattern.DOTALL); } catch (PatternSyntaxException e) { throw new BadRequestException("Invalid regular expression: " + e.getMessage(), e); } final Matcher matcher = pattern.matcher(example); boolean matched = matcher.find(); // Get the first matched group. final RegexTesterResponse.Match match; if (matched && matcher.groupCount() > 0) { match = RegexTesterResponse.Match.create(matcher.group(1), matcher.start(1), matcher.end(1)); } else { match = null; } return RegexTesterResponse.create(matched, match, regex, example); } }
public static String capture(String value, String regexExpression) throws RendererException { try { Pattern p = Pattern.compile(regexExpression); // Pull the value out of the location Matcher m = p.matcher(value); boolean matchFound = m.find(); String result = ""; if (matchFound) { for (int groupIndex = 1; groupIndex <= m.groupCount(); groupIndex++) result += m.group(groupIndex); } return result; } catch (PatternSyntaxException e) { throw new RendererException("invalid capturing expression " + regexExpression + ": " + e.getMessage()); } }
public String render(String text) { if (StringUtils.isBlank(text)) { return ""; } 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; }
public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException { MavenProject project; try { project = (MavenProject) helper.evaluate("${project}"); if (project.getUrl() == null) { throw new EnforcerRuleException("The project URL is not defined"); } Matcher matcher = Pattern.compile(regex).matcher(project.getUrl()); if (!matcher.matches()) { throw new EnforcerRuleException( "The project URL " + project.getUrl() + " does not match the required regex: " + regex); } } catch (ExpressionEvaluationException e) { throw new EnforcerRuleException("Unable to lookup an expression " + e.getLocalizedMessage(), e); } catch (PatternSyntaxException e) { throw new EnforcerRuleException("Invalid regex \"" + regex + "\": " + e.getLocalizedMessage(), e); } }
import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; public class RegexTester { public static void main(String[] arguments) { String userInputPattern = arguments[0]; try { Pattern.compile(userInputPattern); } catch (PatternSyntaxException exception) { System.err.println(exception.getDescription()); System.exit(1); } System.out.println("Syntax is ok."); } }
Pattern.compile( props.getProperty(RESOURCE_NAME_REGEX) + "!/META-INF/MANIFEST.MF"); } catch (PatternSyntaxException e) { LOGGER.log(java.util.logging.Level.SEVERE, e.getLocalizedMessage(), e); throw e;
public RegexpMatcher(String term, boolean caseInsensitive) { super(caseInsensitive); Pattern regexp; try { regexp = Pattern.compile(term, caseInsensitive ? Pattern.CASE_INSENSITIVE : 0 ); } catch ( PatternSyntaxException e) { regexp = null; LOGGER.log(Level.WARNING, "RegexpMatcher: {0}", e.getMessage() ); } this.termRegexp = regexp; }
@Override public void execute(Context context) { String s = context.getStringArgument(0); String regex = context.getStringArgument(1); try { Pattern pattern = validatePattern(regex); Matcher matcher = pattern.matcher(s); BBoolean matches = new BBoolean(matcher.matches()); context.setReturnValues(matches); } catch (PatternSyntaxException e) { context.setReturnValues(BuiltInUtils.createStringError(context, e.getMessage())); } } }
NamedGroupPattern(Pattern pattern, List<String> groupNames) { this.pattern = pattern; this.groupNames = groupNames; int groupNamesSize = groupNames.size(); if (groupNamesSize != 0 && (pattern.matcher("").groupCount() != groupNamesSize)) { throw new PatternSyntaxException( "Inconsistant named group count. The number of named groups must match the number of groups in the pattern.", pattern.toString(), -1); } }
public PhonologicalRules(String[] data) { try { key = Pattern.compile(data[0]); } catch (PatternSyntaxException e) { System.err.println("Cannot compile regular expression `" + data[0] + "':"); e.printStackTrace(); } precise = data[1]; normal = data[2]; sloppy = data[3]; }
private String computeReplacementString(Pattern pattern, String originalText, String replacementText) { if (pattern != null) { try { return pattern.matcher(originalText).replaceFirst(replacementText); } catch (IndexOutOfBoundsException ex) { throw new PatternSyntaxException(ex.getLocalizedMessage(), replacementText, -1); } } return replacementText; }
/** * Set the bug pattern to a new value * * @param bugPattern the new pattern * @throws PatternSyntaxException when the pattern is not a valid regexp or * does not contain at least one capture group and the group does not * contain a single character */ public void setBugPattern(String bugPattern) throws PatternSyntaxException { if (!bugPattern.matches(PATTERN_SINGLE_GROUP)) { throw new PatternSyntaxException(PATTERN_MUST_CONTAIN_GROUP, bugPattern, 0); } this.bugPattern = Pattern.compile(bugPattern).toString(); }
/** * Checks text value of JTextField if it is a valid regular expression. If not, focus is set to the text field and * an alert is shown. * * @param textfield * the text field with the regular expression * @return true if regular expression is valid, false otherwise */ private boolean checkRegExp(JTextField textfield) { try { Pattern.compile(textfield.getText()); } catch (PatternSyntaxException e) { textfield.setCaretPosition(e.getIndex()); JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), OStrings.getString("HTML_ERROR_CUSTOMREGEXP_TITLE"), JOptionPane.ERROR_MESSAGE); textfield.grabFocus(); return false; } return true; }
public boolean init( StepMetaInterface smi, StepDataInterface sdi ) { meta = (SplitFieldToRowsMeta) smi; data = (SplitFieldToRowsData) sdi; if ( super.init( smi, sdi ) ) { data.rownr = 1L; try { String delimiter = Const.nullToEmpty( meta.getDelimiter() ); if ( meta.isDelimiterRegex() ) { data.delimiterPattern = Pattern.compile( environmentSubstitute( delimiter ) ); } else { data.delimiterPattern = Pattern.compile( Pattern.quote( environmentSubstitute( delimiter ) ) ); } } catch ( PatternSyntaxException pse ) { log.logError( pse.getMessage() ); throw pse; } return true; } return false; } }
@Override public void saveOrUpdate(SpamRule rule) throws NotFoundException { try { Pattern.compile(rule.getRegex()); } catch (PatternSyntaxException ex){ throw new ValidationException(Sets.newHashSet(new ValidationError("regex", ex.getDescription() + " near index " + ex.getIndex()))); } getDao().saveOrUpdate(rule); }
/** * 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. * * @return The full detail message */ @Override /*@Pure*/ public String getMessage() { return pse.getMessage(); }
/** * Constructs a new CheckedPatternSyntaxException. * * @param desc A description of the error * @param regex The erroneous pattern * @param index The approximate index in the pattern of the error, * or {@code -1} if the index is not known */ public CheckedPatternSyntaxException(String desc, String regex, int index) { this(new PatternSyntaxException(desc, regex, index)); }