use of com.google.gwt.regexp.shared.RegExp 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.RegExp 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.RegExp 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());
}
use of com.google.gwt.regexp.shared.RegExp in project rstudio by rstudio.
the class RmdYamlData method getOffsetParseError.
// Returns the parse error, with the line number adjusted by the given
// offset.
public final String getOffsetParseError(int offsetline) {
String error = getParseError();
String lineRegex = "line (\\d+),";
RegExp reg = RegExp.compile(lineRegex);
MatchResult result = reg.exec(error);
if (result == null || result.getGroupCount() < 2)
return getParseError();
else {
Integer newLine = Integer.parseInt(result.getGroup(1)) + offsetline;
return error.replaceAll(lineRegex, "line " + newLine.toString() + ",");
}
}
use of com.google.gwt.regexp.shared.RegExp in project rstudio by rstudio.
the class BreakpointManager method onConsoleWriteInput.
@Override
public void onConsoleWriteInput(ConsoleWriteInputEvent event) {
// when a file is sourced, replay all the breakpoints in the file.
RegExp sourceExp = RegExp.compile("source(.with.encoding)?\\('([^']*)'.*");
MatchResult fileMatch = sourceExp.exec(event.getInput());
if (fileMatch == null || fileMatch.getGroupCount() == 0) {
return;
}
String path = FilePathUtils.normalizePath(fileMatch.getGroup(2), workbench_.getCurrentWorkingDir().getPath());
resetBreakpointsInPath(path, true);
}
Aggregations