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 boolean getGroup(String name, TextBuffer tb) { return getGroup(name, tb, 0); } public boolean getGroup(String name, TextBuffer tb, int modes) {
public String group(int group) { return matcher.group(group); }
public Matcher(Pattern regex, CharSequence target) { setPattern(regex); setTarget(target); }
@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); } }
private Element makeQueue(Matcher refMatcher) { if (refMatcher.find()) { Element element; int modes = 0; if (refMatcher.isCaptured(NAME_ID)) { if (refMatcher.isCaptured(MODE_ID)) { String md = refMatcher.group(MODE_ID); for (int i = 0; i < md.length(); i++) { switch (md.charAt(i)) { char c = refMatcher.charAt(0, NAME_ID); if (c == '&') { element = new IntRefHandler(refMatcher.prefix(), 0, modes); } else if (Character.isDigit(c)) { element = new IntRefHandler(refMatcher.prefix(), new Integer(refMatcher.group(NAME_ID)), modes); } else element = new StringRefHandler(refMatcher.prefix(), refMatcher.group(NAME_ID), modes); } else { element = new PlainElement(refMatcher.prefix(), refMatcher.group(ESC_ID)); refMatcher.setTarget(refMatcher, MatchResult.SUFFIX); element.next = makeQueue(refMatcher); return element; } else return new PlainElement(refMatcher.target());
/** * 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; }
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(); }
private void check() { hasMore = find(options); checked = true; }
/** * 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 Matcher region(int start, int end) { if ((start < 0) || (start > matcher.targetEnd())) throw new IndexOutOfBoundsException("start"); if ((end < 0) || (end > matcher.targetEnd())) throw new IndexOutOfBoundsException("end"); if (start > end) throw new IndexOutOfBoundsException("start > end"); matcher.setTarget(matcher.target(), start, end - start); return this; }
/** * Returns the start index of the match. * * @return The index of the first character matched */ @Override public int start() { return matcher.start(); }
/** * The same as findAll(int), but with default behaviour; */ public MatchIterator findAll() { return findAll(0); }
public Matcher usePattern(Pattern newPattern) { this.matcher.setPattern(newPattern.i); this.pattern = newPattern; this.internalPattern = newPattern.i; return this; }
/** */ public final boolean isCaptured(String groupName) { Integer id = re.groupId(groupName); if (id == null) throw new IllegalArgumentException("unknown group: \"" + groupName + "\""); return isCaptured(id); }
public Matcher reset(CharSequence input) { matcher.setTarget(input); return reset(); }
/** * All matchers have the state used by Pattern during a match. */ Matcher(Pattern parent, CharSequence text) { pattern = parent; matcher = new regexodus.Matcher(parent.internal, text); }