Search in sources :

Example 1 with MatchResult

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

Example 2 with MatchResult

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

Example 3 with MatchResult

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

Example 4 with MatchResult

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

Example 5 with MatchResult

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

Aggregations

MatchResult (com.google.gwt.regexp.shared.MatchResult)47 RegExp (com.google.gwt.regexp.shared.RegExp)23 ArrayList (java.util.ArrayList)7 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)3 ProjectId (edu.stanford.bmir.protege.web.shared.project.ProjectId)3 JsArrayString (com.google.gwt.core.client.JsArrayString)1 Node (com.google.gwt.dom.client.Node)1 PreElement (com.google.gwt.dom.client.PreElement)1 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)1 ClickHandler (com.google.gwt.event.dom.client.ClickHandler)1 SafeHtml (com.google.gwt.safehtml.shared.SafeHtml)1 BasicOverlayType (cz.metacentrum.perun.webgui.model.BasicOverlayType)1 ItemTexts (cz.metacentrum.perun.webgui.model.ItemTexts)1 AutoCompletionChoice (edu.stanford.bmir.gwtcodemirror.client.AutoCompletionChoice)1 AutoCompletionResult (edu.stanford.bmir.gwtcodemirror.client.AutoCompletionResult)1 EditorPosition (edu.stanford.bmir.gwtcodemirror.client.EditorPosition)1 CollectionId (edu.stanford.bmir.protege.web.shared.collection.CollectionId)1 CollectionItem (edu.stanford.bmir.protege.web.shared.collection.CollectionItem)1 FormId (edu.stanford.bmir.protege.web.shared.form.FormId)1 GetUserIdCompletionsAction (edu.stanford.bmir.protege.web.shared.itemlist.GetUserIdCompletionsAction)1