/** * Returns a new {@code MatchResult} that corresponds to a successful match. Apache Harmony (used * in Android) requires a successful match in order to generate a {@code MatchResult}: * http://goo.gl/5VQFmC */ private static MatchResult newMatchResult() { Matcher matcher = Pattern.compile(".").matcher("X"); matcher.find(); return matcher.toMatchResult(); }
/** * Returns {@code true} if the exclusion {@link Pattern} at index {@code patternIndex} * matches the supplied candidate {@code String}. */ @Override protected boolean matchesExclusion(String candidate, int patternIndex) { Matcher matcher = this.compiledExclusionPatterns[patternIndex].matcher(candidate); return matcher.matches(); }
String[] parts = string.split(Pattern.quote(".")); // Split on period.
private static void assertContainsRegex(String expectedRegex, String actual) { Pattern pattern = Pattern.compile(expectedRegex); Matcher matcher = pattern.matcher(actual); if (!matcher.find()) { String actualDesc = (actual == null) ? "null" : ('<' + actual + '>'); fail("expected to contain regex:<" + expectedRegex + "> but was:" + actualDesc); } } }
/** * Returns the length of the given pattern, where template variables are considered to be 1 long. */ public int getLength() { if (this.length == null) { this.length = (this.pattern != null ? VARIABLE_PATTERN.matcher(this.pattern).replaceAll("#").length() : 0); } return this.length; } }
Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+ Account[] accounts = AccountManager.get(context).getAccounts(); for (Account account : accounts) { if (emailPattern.matcher(account.name).matches()) { String possibleEmail = account.name; ... } }
/** * Constructs a pattern file name filter object. * * @param patternStr the pattern string on which to filter file names * @throws PatternSyntaxException if pattern compilation fails (runtime) */ public PatternFilenameFilter(String patternStr) { this(Pattern.compile(patternStr)); }
@Override public boolean find() { return matcher.find(); }
@Override public int start() { return matcher.start(); } }
@Override public String pattern() { return pattern.pattern(); }
/** * Returns {@code true} if the {@link Pattern} at index {@code patternIndex} * matches the supplied candidate {@code String}. */ @Override protected boolean matches(String pattern, int patternIndex) { Matcher matcher = this.compiledPatterns[patternIndex].matcher(pattern); return matcher.matches(); }
/** * Compiles the supplied {@code String[]} into an array of * {@link Pattern} objects and returns that array. */ private Pattern[] compilePatterns(String[] source) throws PatternSyntaxException { Pattern[] destination = new Pattern[source.length]; for (int i = 0; i < source.length; i++) { destination[i] = Pattern.compile(source[i]); } return destination; }
/** * Validate the jsonp query parameter value. The default implementation * returns true if it consists of digits, letters, or "_" and ".". * Invalid parameter values are ignored. * * @param value the query param value, never {@code null} */ protected boolean isValidJsonpQueryParam(String value) { return CALLBACK_PARAM_PATTERN.matcher(value).matches(); }
/** * Indicate whether the given URI matches this template. * @param uri the URI to match to * @return {@code true} if it matches; {@code false} otherwise */ public boolean matches(@Nullable String uri) { if (uri == null) { return false; } Matcher matcher = this.matchPattern.matcher(uri); return matcher.matches(); }