use of com.google.gwt.regexp.shared.MatchResult in project che by eclipse.
the class NameGenerator method removeCopyPrefix.
private static String removeCopyPrefix(String name) {
RegExp regexp = RegExp.compile("Copy\\d* of (.*)");
MatchResult matchResult = regexp.exec(name);
// do not find prefix, return as this
if (matchResult == null || matchResult.getGroupCount() != 2) {
return name;
}
return matchResult.getGroup(1);
}
use of com.google.gwt.regexp.shared.MatchResult in project che by eclipse.
the class Bytes method splitValueAndUnit.
/**
* Extract unit and values from the given string
* @param humanSize the value to analyze
* @return array with first index the value, second one the unit
*/
protected static Pair<Double, Unit> splitValueAndUnit(String humanSize) {
MatchResult matchResult = UNIT_PATTERN.exec(humanSize);
if (matchResult.getGroupCount() != 3 || matchResult.getGroup(2).isEmpty()) {
throw new IllegalArgumentException("Unable to get unit in the given value '" + humanSize + "'");
}
Double val = Double.parseDouble(matchResult.getGroup(1).trim());
Unit unitVal = Unit.valueOf(matchResult.getGroup(2).trim());
Pair<Double, Unit> value = new Pair<>(val, unitVal);
return value;
}
use of com.google.gwt.regexp.shared.MatchResult in project che by eclipse.
the class RegExpUtils method findMatchBeforeIndex.
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;
}
use of com.google.gwt.regexp.shared.MatchResult in project che by eclipse.
the class RegExpUtils method createRegExpStringForWildcardPattern.
/**
* 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.
*
* 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();
}
use of com.google.gwt.regexp.shared.MatchResult in project gerrit by GerritCodeReview.
the class SafeHtml method replaceAll.
/**
* Replace all find/replace pairs in the list in a single pass.
*
* @param findReplaceList find/replace pairs to use.
* @return a new string, after the replacements have been made.
*/
public <T> SafeHtml replaceAll(List<? extends FindReplace> findReplaceList) {
if (findReplaceList == null || findReplaceList.isEmpty()) {
return this;
}
StringBuilder pat = new StringBuilder();
Iterator<? extends FindReplace> it = findReplaceList.iterator();
while (it.hasNext()) {
FindReplace fr = it.next();
pat.append(fr.pattern().getSource());
if (it.hasNext()) {
pat.append('|');
}
}
StringBuilder result = new StringBuilder();
RegExp re = RegExp.compile(pat.toString(), "g");
String orig = asString();
int index = 0;
MatchResult mat;
while ((mat = re.exec(orig)) != null) {
String g = mat.getGroup(0);
// Re-run each candidate to find which one matched.
for (FindReplace fr : findReplaceList) {
if (fr.pattern().test(g)) {
try {
String repl = fr.replace(g);
result.append(orig.substring(index, mat.getIndex()));
result.append(repl);
} catch (IllegalArgumentException e) {
continue;
}
index = mat.getIndex() + g.length();
break;
}
}
}
result.append(orig.substring(index, orig.length()));
return asis(result.toString());
}
Aggregations