private void check() { hasMore = find(options); checked = true; }
private void check() { hasMore = find(options); checked = true; }
public boolean find() { return matcher.find(); }
/** * Attempts to find the next subsequence of the input sequence that matches * the pattern. * * <p> This method starts at the beginning of this matcher's region, or, if * a previous invocation of the method was successful and the matcher has * not since been reset, at the first character not matched by the previous * match. * * <p> If the match succeeds then more information can be obtained via the * <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods. </p> * * @return <tt>true</tt> if, and only if, a subsequence of the input * sequence matches this matcher's pattern */ public boolean find() { return matcher.find(); }
@Override public int count() { if (!checked) check(); if (!hasMore) return 0; int c = 1; while (find(options)) c++; checked = false; return c; }
public int count() { if (!checked) check(); if (!hasMore) return 0; int c = 1; while (find(options)) c++; checked = false; return c; } };
public boolean find(int start) { int limit = matcher.targetEnd(); if ((start < 0) || (start > limit)) throw new IndexOutOfBoundsException("Illegal start index"); reset(); matcher.setPosition(start); return matcher.find(); }
@Override public ArrayList<String> asList() { if (!checked) check(); ArrayList<String> found = new ArrayList<String>(16); if (!hasMore) return found; found.add(Matcher.this.group()); while (find(options)) found.add(Matcher.this.group()); checked = false; return found; } };
protected static boolean checkAll(CharSequence testing, Pattern[] checks) { CharSequence fixed = removeAccents(testing); for (int i = 0; i < checks.length; i++) { if (checks[i].matcher(fixed).find()) return false; } return true; }
/** * Checks a CharSequence, such as a String, against an overzealous vulgarity filter, returning true if the text * could contain vulgar elements or words that could seem vulgar or juvenile. The idea here is that false positives * are OK as long as there are very few false negatives (missed vulgar words). Does not check punctuation or numbers * that could look like letters. * @param testing the text, as a CharSequence such as a String, to check * @return true if the text could contain a vulgar or juvenile element; false if it probably doesn't */ public static boolean checkVulgarity(CharSequence testing) { CharSequence fixed = removeAccents(testing); for (int i = 0; i < vulgarChecks.length; i++) { if (vulgarChecks[i].matcher(fixed).find()) { System.out.println(vulgarChecks[i]); return true; } } return false; }
public static int indexOf(CharSequence text, Pattern regex) { matcher.setPattern(regex); matcher.setTarget(text); if(!matcher.find()) return -1; return matcher.start(); } public static int indexOf(CharSequence text, String regex)
/** * Resets this matcher and then attempts to find the next subsequence of * the input sequence that matches the pattern, starting at the specified * index. * * <p> If the match succeeds then more information can be obtained via the * <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods, and subsequent * invocations of the {@link #find()} method will start at the first * character not matched by this match. </p> * * @throws IndexOutOfBoundsException * If start is less than zero or if start is greater than the * length of the input sequence. * * @return <tt>true</tt> if, and only if, a subsequence of the input * sequence starting at the given index matches this matcher's * pattern */ public boolean find(int start) { int limit = matcher.targetEnd(); if ((start < 0) || (start > limit)) throw new IndexOutOfBoundsException("Illegal start index"); reset(); matcher.setPosition(start); return matcher.find(); }
public static int indexOf(CharSequence text, String regex) { matcher.setPattern(Pattern.compile(regex)); matcher.setTarget(text); if(!matcher.find()) return -1; return matcher.start(); }
public static int indexOf(CharSequence text, Pattern regex, int beginIndex) { matcher.setPattern(regex); matcher.setTarget(text); matcher.setPosition(beginIndex); if(!matcher.find()) return -1; return matcher.start(); } public static int indexOf(CharSequence text, String regex, int beginIndex)
public static int indexOf(CharSequence text, String regex, int beginIndex) { matcher.setPattern(Pattern.compile(regex)); matcher.setTarget(text); matcher.setPosition(beginIndex); if(!matcher.find()) return -1; return matcher.start(); } public static int indexOf(CharSequence text, Pattern regex)
/** * Replaces all occurrences of a matcher's pattern in a matcher's target * by a given substitution appending the result to a buffer.<br> * The substitution starts from current matcher's position, current match * not included. */ public static int replace(Matcher m, Substitution substitution, TextBuffer dest) { boolean firstPass = true; int c = 0; while (m.find()) { if (m.end() == 0 && !firstPass) continue; //allow to replace at "^" if (m.start() > 0) m.getGroup(MatchResult.PREFIX, dest); substitution.appendSubstitution(m, dest); c++; m.setTarget(m, MatchResult.SUFFIX); firstPass = false; } m.getGroup(MatchResult.TARGET, dest); return c; }
/** * Replaces all occurrences of a matcher's pattern in a matcher's target * by a given substitution appending the result to a buffer.<br> * The substitution starts from current matcher's position, current match * not included. */ public static int replace(Matcher m, Substitution substitution, TextBuffer dest) { boolean firstPass = true; int c = 0; while (m.find()) { if (m.end() == 0 && !firstPass) continue; //allow to replace at "^" if (m.start() > 0) m.getGroup(MatchResult.PREFIX, dest); substitution.appendSubstitution(m, dest); c++; m.setTarget(m, MatchResult.SUFFIX); firstPass = false; } m.getGroup(MatchResult.TARGET, dest); return c; }
/** * Replaces the first n occurrences of a matcher's pattern, where n is equal to count, * in a matcher's target by a given substitution, appending the result to a buffer. * <br> * The substitution starts from current matcher's position, current match not included. * @param m a Matcher * @param substitution a Substitution, typically a PerlSubstitution * @param dest the TextBuffer this will write to; see Replacer.wrap() * @param count the number of replacements to attempt * @return the number of replacements performed */ public static int replace(Matcher m, Substitution substitution, TextBuffer dest, int count) { boolean firstPass = true; int c = 0; while (c < count && m.find()) { if (m.end() == 0 && !firstPass) continue; //allow to replace at "^" if (m.start() > 0) m.getGroup(MatchResult.PREFIX, dest); substitution.appendSubstitution(m, dest); c++; m.setTarget(m, MatchResult.SUFFIX); firstPass = false; } m.getGroup(MatchResult.TARGET, dest); return c; }
/** * Replaces the first n occurrences of a matcher's pattern, where n is equal to count, * in a matcher's target by a given substitution, appending the result to a buffer. * <br> * The substitution starts from current matcher's position, current match not included. * @param m a Matcher * @param substitution a Substitution, typically a PerlSubstitution * @param dest the TextBuffer this will write to; see Replacer.wrap() * @param count the number of replacements to attempt * @return the number of replacements performed */ public static int replace(Matcher m, Substitution substitution, TextBuffer dest, int count) { boolean firstPass = true; int c = 0; while (c < count && m.find()) { if (m.end() == 0 && !firstPass) continue; //allow to replace at "^" if (m.start() > 0) m.getGroup(MatchResult.PREFIX, dest); substitution.appendSubstitution(m, dest); c++; m.setTarget(m, MatchResult.SUFFIX); firstPass = false; } m.getGroup(MatchResult.TARGET, dest); return c; }
@Override public void appendSubstitution(MatchResult match, TextBuffer dest) { if(match instanceof Matcher) { wordMatcher.setTarget((Matcher)match, 1); } else { wordMatcher.setTarget(match.targetChars(), match.start(1) + match.targetStart(), match.length(1)); } while (wordMatcher.find()) { wordMatcher.getGroup(MatchResult.PREFIX, dest); dest.append(lookup(wordMatcher.group())); wordMatcher.setTarget(wordMatcher, MatchResult.SUFFIX); } wordMatcher.getGroup(MatchResult.TARGET, dest); } }