public void reset() { matcher.setPosition(0); }
public void reset() { matcher.setPosition(0); }
/** * Tells whether a current target matches the whole pattern. * For example the following yields the <code>true</code>:<pre> * Pattern p=new Pattern("\\w+"); * p.matcher("a").matches(); * p.matcher("ab").matches(); * p.matcher("abc").matches(); * </pre> * and the following yields the <code>false</code>:<pre> * p.matcher("abc def").matches(); * p.matcher("bcd ").matches(); * p.matcher(" bcd").matches(); * p.matcher("#xyz#").matches(); * </pre> * * @return whether a current target matches the whole pattern. */ public final boolean matches() { if (called) setPosition(0); return search(ANCHOR_START | ANCHOR_END); }
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(); }
/** * Tells whether a current target matches the whole pattern. * For example the following yields the <code>true</code>:<pre> * Pattern p=new Pattern("\\w+"); * p.matcher("a").matches(); * p.matcher("ab").matches(); * p.matcher("abc").matches(); * </pre> * and the following yields the <code>false</code>:<pre> * p.matcher("abc def").matches(); * p.matcher("bcd ").matches(); * p.matcher(" bcd").matches(); * p.matcher("#xyz#").matches(); * </pre> * * @return whether a current target matches the whole pattern. */ public final boolean matches() { if (called) setPosition(0); return search(ANCHOR_START | ANCHOR_END); }
/** * Tells whether the entire target matches the beginning of the pattern. * The whole pattern is also regarded as its beginning.<br> * This feature allows to find a mismatch by examining only a beginning part of * the target (as if the beginning of the target doesn't match the beginning of the pattern, then the entire target * also couldn't match).<br> * For example the following assertions yield <code>true</code>:<pre> * Pattern p=new Pattern("abcd"); * p.matcher("").matchesPrefix(); * p.matcher("a").matchesPrefix(); * p.matcher("ab").matchesPrefix(); * p.matcher("abc").matchesPrefix(); * p.matcher("abcd").matchesPrefix(); * </pre> * and the following yield <code>false</code>:<pre> * p.matcher("b").isPrefix(); * p.matcher("abcdef").isPrefix(); * p.matcher("x").isPrefix(); * </pre> * * @return true if the entire target matches the beginning of the pattern */ public final boolean matchesPrefix() { setPosition(0); return search(ANCHOR_START | ACCEPT_INCOMPLETE | ANCHOR_END); }
/** * Tells whether the entire target matches the beginning of the pattern. * The whole pattern is also regarded as its beginning.<br> * This feature allows to find a mismatch by examining only a beginning part of * the target (as if the beginning of the target doesn't match the beginning of the pattern, then the entire target * also couldn't match).<br> * For example the following assertions yield <code>true</code>:<pre> * Pattern p=new Pattern("abcd"); * p.matcher("").matchesPrefix(); * p.matcher("a").matchesPrefix(); * p.matcher("ab").matchesPrefix(); * p.matcher("abc").matchesPrefix(); * p.matcher("abcd").matchesPrefix(); * </pre> * and the following yield <code>false</code>:<pre> * p.matcher("b").isPrefix(); * p.matcher("abcdef").isPrefix(); * p.matcher("x").isPrefix(); * </pre> * * @return true if the entire target matches the beginning of the pattern */ public final boolean matchesPrefix() { setPosition(0); return search(ANCHOR_START | ACCEPT_INCOMPLETE | ANCHOR_END); }
/** * 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, 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)