本文整理匯總了Java中com.google.gwt.regexp.shared.RegExp.setLastIndex方法的典型用法代碼示例。如果您正苦於以下問題:Java RegExp.setLastIndex方法的具體用法?Java RegExp.setLastIndex怎麽用?Java RegExp.setLastIndex使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.gwt.regexp.shared.RegExp
的用法示例。
在下文中一共展示了RegExp.setLastIndex方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: replace
import com.google.gwt.regexp.shared.RegExp; //導入方法依賴的package包/類
/**
* Execute a regular expression and invoke a callback for each match
* occurance. The return value of the callback is substituted for the match.
*
* @param expression a compiled regular expression
* @param text a String on which to perform replacement
* @param replacer a callback that maps matched strings into new values
*/
private static String replace(RegExp expression, String text,
RegExpReplacer replacer) {
expression.setLastIndex(0);
MatchResult mresult = expression.exec(text);
StringBuffer toReturn = new StringBuffer();
int lastIndex = 0;
while (mresult != null) {
toReturn.append(text.substring(lastIndex, mresult.getIndex()));
toReturn.append(replacer.replace(mresult.getGroup(0)));
lastIndex = mresult.getIndex() + 1;
mresult = expression.exec(text);
}
toReturn.append(text.substring(lastIndex));
return toReturn.toString();
}
示例2: getWordAtOffset
import com.google.gwt.regexp.shared.RegExp; //導入方法依賴的package包/類
public Position getWordAtOffset(Document document, int offset) {
if (document == null) {
return null;
}
RegExp regExp = RegExp.compile(COMMON_WORD_REGEXP, "g");
int line = document.getLineAtOffset(offset);
String lineContent = document.getLineContent(line);
int lineStart = document.getLineStart(line);
int pos = offset - lineStart;
int start = lineContent.lastIndexOf(' ', pos - 1) + 1;
regExp.setLastIndex(start);
MatchResult matchResult;
while ((matchResult = regExp.exec(lineContent)) != null) {
if (matchResult.getIndex() <= pos && regExp.getLastIndex() >= pos) {
return new Position(matchResult.getIndex() + lineStart, matchResult.getGroup(0).length());
}
}
return null;
}
示例3: findMatchBeforeIndex
import com.google.gwt.regexp.shared.RegExp; //導入方法依賴的package包/類
public static MatchResult findMatchBeforeIndex(
RegExp regexp, String text, int exclusiveEndIndex) {
regexp.setLastIndex(0);
// Find the last match without going over our startIndex
MatchResult lastMatch = null;
for (MatchResult result = regexp.exec(text);
result != null && result.getIndex() < exclusiveEndIndex;
result = regexp.exec(text)) {
lastMatch = result;
}
return lastMatch;
}
示例4: createRegExpStringForWildcardPattern
import com.google.gwt.regexp.shared.RegExp; //導入方法依賴的package包/類
/**
* Creates a regular expression which will match the given wildcard pattern
*
* <p>Backslashes can be used to escape a wildcard character and make it a literal; likewise,
* backslashes before wildcard characters can be escaped.
*/
private static String createRegExpStringForWildcardPattern(String wildcardPattern) {
String escaped = regexpWildcardEscape.replace(wildcardPattern, "\\$&");
/**
* We have already run the pattern through the naive regex escape which escapes all characters
* except the * and ?. This leads to double escaped \ characters that we have to inspect to
* determine if the user escaped the wildcard or if we should replace it with it's regex
* equivalent.
*
* <p>NOTE: * is replaced with \S+ (matches all non-whitespace characters) and ? is replaced
* with a single \S to match any non-whitespace
*/
RegExp mimicLookbehind = RegExp.compile("([\\\\]*)([?*])", "g");
StringBuilder wildcardStr = new StringBuilder(escaped);
for (MatchResult match = mimicLookbehind.exec(wildcardStr.toString());
match != null;
match = mimicLookbehind.exec(wildcardStr.toString())) {
// in some browsers an optional group is null, in others its empty string
if (match.getGroup(1) != null && !match.getGroup(1).isEmpty()) {
// We undo double-escaping of backslashes performed by the naive escape
int offset = match.getGroup(1).length() / 2;
wildcardStr.delete(match.getIndex(), match.getIndex() + offset);
/*
* An even number of slashes means the wildcard was not escaped so we
* must replace it with its regex equivalent.
*/
if (offset % 2 == 0) {
if (match.getGroup(2).equals("?")) {
wildcardStr.replace(match.getIndex() + offset, match.getIndex() + offset + 1, "\\S");
// we added 1 more character, so we remove 1 less from the index
offset -= 1;
} else {
wildcardStr.replace(match.getIndex() + offset, match.getIndex() + offset + 1, "\\S+");
// we added 2 characters, so we need to remove 2 less from the index
offset -= 2;
}
}
mimicLookbehind.setLastIndex(mimicLookbehind.getLastIndex() - offset);
} else if (match.getGroup(2).equals("?")) {
wildcardStr.replace(match.getIndex(), match.getIndex() + 1, "\\S");
mimicLookbehind.setLastIndex(mimicLookbehind.getLastIndex() + 1);
} else {
wildcardStr.replace(match.getIndex(), match.getIndex() + 1, "\\S+");
mimicLookbehind.setLastIndex(mimicLookbehind.getLastIndex() + 2);
}
}
return wildcardStr.toString();
}
示例5: findMatchAfterIndex
import com.google.gwt.regexp.shared.RegExp; //導入方法依賴的package包/類
/** Find the next match after exclusiveStartIndex. */
public static MatchResult findMatchAfterIndex(
RegExp regexp, String text, int exclusiveStartIndex) {
regexp.setLastIndex(exclusiveStartIndex + 1);
return regexp.exec(text);
}
示例6: resetAndTest
import com.google.gwt.regexp.shared.RegExp; //導入方法依賴的package包/類
/** Resets the RegExp lastIndex to 0 before testing. This is only useful for global RegExps. */
public static boolean resetAndTest(RegExp regexp, String input) {
regexp.setLastIndex(0);
return regexp.test(input);
}
示例7: resetAndGetNumberOfMatches
import com.google.gwt.regexp.shared.RegExp; //導入方法依賴的package包/類
/**
* Resets the RegExp lastIndex to 0 and returns the number of matches found in a string by a
* regexp. If the regexp is not a global regexp this will return a maximum of 1. This does not
* setLastIndex(0) automatically, you must do it manually.
*
* @returns number of matches
*/
public static int resetAndGetNumberOfMatches(RegExp regexp, String input) {
regexp.setLastIndex(0);
return getNumberOfMatches(regexp, input);
}