Example usage for java.util.regex Matcher start

List of usage examples for java.util.regex Matcher start

Introduction

In this page you can find the example usage for java.util.regex Matcher start.

Prototype

public int start() 

Source Link

Document

Returns the start index of the previous match.

Usage

From source file:io.wcm.tooling.netbeans.sightly.completion.classLookup.ProvidedClassesCompleter.java

@Override
public int indexOfStartCharacter(char[] line) {
    final String text = String.copyValueOf(line);
    final Matcher matcher = LAST_DOLLAR_CURLYBRACE.matcher(text);

    if (matcher.find()) {
        final int beginIndex = matcher.start();
        final Matcher variableMatcher = LAST_VARIABLE.matcher(text);
        if (variableMatcher.find(beginIndex)) {
            return variableMatcher.start() - 1;
        }/*from   w  ww. ja  v a2s .  co m*/
    }
    return -1;
}

From source file:gov.nih.nci.cabig.caaers.utils.ranking.Ranker.java

/**
 * Will rank an object, based on the following rules :-
 *  1) If there is a full text match - Highest rank "WHOLE_SENTENCE_MATCH" is given.
 *  1) Starting of the sentence if match - 2nd Highest rank "BEGINING_OF_SENTENCE" is given.
 *  2) Begining of any word match, - 3rd Highest rank "BEGINING_OF_WORD" is given.
 *  3) Anywhere in the sentence match - The lowest rank PART_OF_SENTENCE is given.
 * //from w  w  w  .  j  a va2  s  .c  o m
 * @param obj
 * @return
 */
public <T extends Object> RankedObject rank(T obj, Serializer<T> serializer) {
    RankedObject<T> rankedObject = new RankedObject(obj);
    String str = serializer.serialize(obj);
    int l = str.length();

    //whole sentence
    if (StringUtils.equalsIgnoreCase(str, searchStr)) {
        rankedObject.addToRank(WHOLE_SENTENCE_MATCH);
    }

    Matcher m = p.matcher(str);
    if (m.find()) {

        int start = m.start();
        if (start == 0) {
            //begining of sentence
            rankedObject.addToRank(BEGINING_OF_SENTENCE);
        } else {
            int i = start - 1;
            int j = start - 2;
            char iChar = str.charAt(i);

            //begining of sentence.
            if ((j == 0 && str.charAt(j) == '(') || (i == 0 && iChar == '('))
                rankedObject.addToRank(BEGINING_OF_SENTENCE);

            if (iChar == ' ' || iChar == '(') {

                int k = start + patternLength;
                if (k == l || (k < l && (str.charAt(k) == ' ' || str.charAt(k) == ')'))) {
                    //whole word match
                    rankedObject.addToRank(WHOLE_WORD_MATCH);
                }

                //begining of word
                rankedObject.addToRank(BEGINING_OF_WORD);
            }
        }

        //part of sentence
        rankedObject.addToRank(PART_OF_SENTENCE);

        rankedObject.substractFromRank(start);

    }

    return rankedObject;
}

From source file:io.github.seleniumquery.by.common.preparser.NotEqualsAttributeSelectorFix.java

String removeStrings(String input) {
    Pattern p = Pattern.compile("\"([^\"\\\\]|\\\\.)*\"|'([^'\\\\]|\\\\.)*'");
    Matcher m = p.matcher(input);
    StringBuilder sb = new StringBuilder(input);
    while (m.find()) {
        sb.replace(m.start(), m.end(), StringUtils.repeat('_', m.end() - m.start()));
    }/*from  w  w  w  .ja v a  2s  .c o m*/
    return sb.toString();
}

From source file:io.github.seleniumquery.by.common.preparser.NotEqualsAttributeSelectorFix.java

String removeContains(String input) {
    // :contains matches, \:contains dont, \\:contains does
    Pattern p = Pattern.compile(ESCAPED_SLASHES + ":" + "contains\\(.*?" + ESCAPED_SLASHES + "\\)");
    Matcher m = p.matcher(input);
    StringBuilder sb = new StringBuilder(input);
    while (m.find()) {
        sb.replace(m.start(), m.end(), StringUtils.repeat('_', m.end() - m.start()));
    }//from ww  w  .j  a  v  a  2 s  . c  o  m
    return sb.toString();
}

From source file:com.npower.dm.server.synclet.HttpHeaderPhoneNumberDetector.java

public String getPhoneNumber(String deviceExternalID, HttpServletRequest httpRequest, SyncML message) {
    String name = this.getHeaderName();
    if (StringUtils.isEmpty(name)) {
        log.error(/* w w w.j  a  v  a2s .com*/
                "Http header name is null. please specified the name of header for HttpHeaderPhoneNumberDetector.");
        return null;
    }
    String headerValue = httpRequest.getHeader(name);
    if (StringUtils.isEmpty(headerValue)) {
        log.error("Missing value in http header: " + name);
        return null;
    }
    if (StringUtils.isEmpty(this.getPattern())) {
        return headerValue.trim();
    }
    boolean found = Pattern.compile(pattern).matcher(headerValue).find();
    if (found) {
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(headerValue);
        m.find();
        int start = m.start();
        int end = m.end();
        return headerValue.substring(start, end);
    } else {
        return null;
    }
}

From source file:fr.eolya.utils.http.HttpUtils.java

public static String urlRemoveParameters(String url, String paramsToRemove) {
    if (paramsToRemove == null || "".equals(paramsToRemove))
        return url;

    try {// w ww. j  a  v  a2 s .c om
        URL u = new URL(url);
        if (u.getQuery() == null && u.getPath().indexOf(";jsessionid=") == -1)
            return url;
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
        return null;
    }

    try {

        url = url.replace("?&", "?");

        if ("*".equals(paramsToRemove)) {
            int offset = url.lastIndexOf("?");
            if (offset != -1)
                return url.substring(0, offset);
        }

        paramsToRemove = paramsToRemove.replaceAll(" ", "").replaceAll(";", ",");
        String[] aToRemove = paramsToRemove.split(",");
        String tempUrl = url;
        for (int i = 0; i < aToRemove.length; i++) {
            boolean found = true;
            while (found) {
                found = false;
                String re = "[?&;]" + aToRemove[i].toLowerCase() + "[=&]";
                Pattern p = Pattern.compile(re);
                Matcher m = p.matcher(tempUrl.toLowerCase());
                if (m.find()) {
                    found = true;
                    int start = m.start();
                    int stop = start;
                    if ("jsessionid".equals(aToRemove[i].toLowerCase())) {
                        stop = tempUrl.indexOf("?", start + 1);
                        if (stop == -1)
                            stop = tempUrl.indexOf("&", start + 1);
                    } else {
                        stop = tempUrl.indexOf("&", start + 1);
                    }
                    if (stop == -1) {
                        tempUrl = tempUrl.substring(0, start);
                    } else {
                        String ope = tempUrl.substring(start, start + 1);
                        if (";".equals(ope))
                            ope = "?";
                        tempUrl = tempUrl.substring(0, start) + ope + tempUrl.substring(stop + 1);
                    }
                }
                re = "[?&;]" + aToRemove[i].toLowerCase() + "$";
                p = Pattern.compile(re);
                m = p.matcher(tempUrl.toLowerCase());
                if (m.find()) {
                    found = true;
                    int start = m.start();
                    int stop = start;
                    if ("jsessionid".equals(aToRemove[i].toLowerCase())) {
                        stop = tempUrl.indexOf("?", start + 1);
                        if (stop == -1)
                            stop = tempUrl.indexOf("&", start + 1);
                    } else {
                        stop = tempUrl.indexOf("&", start + 1);
                    }
                    if (stop == -1) {
                        tempUrl = tempUrl.substring(0, start);
                    } else {
                        String ope = tempUrl.substring(start, start + 1);
                        if (";".equals(ope))
                            ope = "?";
                        tempUrl = tempUrl.substring(0, start) + ope + tempUrl.substring(stop + 1);
                    }
                }
            }
        }
        return tempUrl;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

From source file:com.afis.jx.ckfinder.connector.utils.FileUtils.java

/**
 * Checks if path starts with particular regular expression pattern.
 *
 * @param pattern the regular expression used to test the path
 * @param path the string representing path to test against regular expression
 * @return {@code true} if path starts with given pattern, {@code false} otherwise.
 *///from w ww .ja v a2 s  .  c om
private static boolean isStartsWithPattern(Pattern pattern, String path) {
    Matcher m = pattern.matcher(path);
    if (m.find()) {
        return m.start() == 0;
    }
    return false;
}

From source file:io.dataapps.chlorine.pattern.RegexFinder.java

/**
 * Scan the list of inputs using the finders.
 * Return a list of actual matched values.
 * @return a list of matches //from w w w.jav a2  s. co  m
 */
public FinderResult find(String input) {
    List<String> matches = new ArrayList<>();
    Matcher matcher = pattern.matcher(input);
    while (matcher.find()) {
        matches.add(input.substring(matcher.start(), matcher.end()));
    }
    return new FinderResult(matches, replace(input, ""));
}

From source file:net.sf.zekr.engine.search.tanzil.ZeroHighlighter.java

private String getClause(String text, Matcher matcher) {
    int a = text.substring(0, matcher.start() + 1).lastIndexOf(' ');
    int b = text.indexOf(' ', matcher.end() - 1);
    return new String(text.substring(a + 1, b));
}

From source file:com.heliosdecompiler.helios.gui.view.editors.DisassemblerView.java

private static StyleSpans<Collection<String>> computeHighlighting(String text) {
    Matcher matcher = PATTERN.matcher(text);
    int lastKwEnd = 0;
    StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
    while (matcher.find()) {
        String styleClass = matcher.group("KEYWORD") != null ? "keyword"
                : matcher.group("PAREN") != null ? "paren"
                        : matcher.group("BRACE") != null ? "brace"
                                : matcher.group("BRACKET") != null ? "bracket"
                                        : matcher.group("SEMICOLON") != null ? "semicolon"
                                                : matcher.group("STRING") != null ? "string"
                                                        : matcher.group("COMMENT") != null ? "comment"
                                                                : null; /* never happens */
        assert styleClass != null;
        spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
        spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
        lastKwEnd = matcher.end();/*from  w w  w  . java2  s  .  c  o m*/
    }
    spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
    return spansBuilder.create();
}