Search in sources :

Example 1 with RegExp

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);
}
Also used : RegExp(com.google.gwt.regexp.shared.RegExp) MatchResult(com.google.gwt.regexp.shared.MatchResult)

Example 2 with RegExp

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();
}
Also used : RegExp(com.google.gwt.regexp.shared.RegExp) MatchResult(com.google.gwt.regexp.shared.MatchResult)

Example 3 with RegExp

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());
}
Also used : RegExp(com.google.gwt.regexp.shared.RegExp) MatchResult(com.google.gwt.regexp.shared.MatchResult)

Example 4 with RegExp

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() + ",");
    }
}
Also used : RegExp(com.google.gwt.regexp.shared.RegExp) MatchResult(com.google.gwt.regexp.shared.MatchResult)

Example 5 with RegExp

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);
}
Also used : RegExp(com.google.gwt.regexp.shared.RegExp) MatchResult(com.google.gwt.regexp.shared.MatchResult)

Aggregations

RegExp (com.google.gwt.regexp.shared.RegExp)36 MatchResult (com.google.gwt.regexp.shared.MatchResult)24 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)7 JsonCallbackEvents (cz.metacentrum.perun.webgui.json.JsonCallbackEvents)4 BasicOverlayType (cz.metacentrum.perun.webgui.model.BasicOverlayType)4 ArrayList (java.util.ArrayList)4 ChangeHandler (com.google.gwt.event.dom.client.ChangeHandler)3 IsLoginAvailable (cz.metacentrum.perun.webgui.json.usersManager.IsLoginAvailable)3 SetLogin (cz.metacentrum.perun.webgui.json.usersManager.SetLogin)3 TabItem (cz.metacentrum.perun.webgui.tabs.TabItem)3 CustomButton (cz.metacentrum.perun.webgui.widgets.CustomButton)3 Test (org.junit.Test)3 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)2 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)2 KeyDownHandler (com.google.gwt.event.dom.client.KeyDownHandler)2 GetEntityById (cz.metacentrum.perun.webgui.json.GetEntityById)2 CreatePassword (cz.metacentrum.perun.webgui.json.usersManager.CreatePassword)2 GenerateAccount (cz.metacentrum.perun.webgui.json.usersManager.GenerateAccount)2 PerunError (cz.metacentrum.perun.webgui.model.PerunError)2 ExtendedTextBox (cz.metacentrum.perun.webgui.widgets.ExtendedTextBox)2