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:jp.go.nict.langrid.wrapper.ws_1_2.translation.AbstractTranslationService.java

private String toUpperCaseCharacterBehindDelimiter(String source) {
    StringBuilder sb = new StringBuilder(source);
    Pattern p = Pattern.compile("(\\Q*$%*\\E|\\Q*%$*\\E)\\. *[a-z]");
    Matcher m = p.matcher(sb);

    while (m.find()) {
        String sub = sb.substring(m.start(), m.end());
        sb.replace(m.start(), m.end(), sub.toUpperCase());
    }/*from ww  w .j av  a 2  s  . c o m*/

    return sb.toString();
}

From source file:com.norconex.importer.handler.tagger.impl.TextBetweenTagger.java

@Override
protected void tagStringContent(String reference, StringBuilder content, ImporterMetadata metadata,
        boolean parsed, boolean partialContent) {
    int flags = Pattern.DOTALL | Pattern.UNICODE_CASE;
    if (!caseSensitive) {
        flags = flags | Pattern.CASE_INSENSITIVE;
    }/*w w  w.ja v  a2 s. c o m*/
    for (TextBetween between : betweens) {
        List<Pair<Integer, Integer>> matches = new ArrayList<Pair<Integer, Integer>>();
        Pattern leftPattern = Pattern.compile(between.start, flags);
        Matcher leftMatch = leftPattern.matcher(content);
        while (leftMatch.find()) {
            Pattern rightPattern = Pattern.compile(between.end, flags);
            Matcher rightMatch = rightPattern.matcher(content);
            if (rightMatch.find(leftMatch.end())) {
                if (inclusive) {
                    matches.add(new ImmutablePair<Integer, Integer>(leftMatch.start(), rightMatch.end()));
                } else {
                    matches.add(new ImmutablePair<Integer, Integer>(leftMatch.end(), rightMatch.start()));
                }
            } else {
                break;
            }
        }
        for (int i = matches.size() - 1; i >= 0; i--) {
            Pair<Integer, Integer> matchPair = matches.get(i);
            String value = content.substring(matchPair.getLeft(), matchPair.getRight());
            if (value != null) {
                metadata.addString(between.name, value);
            }
        }
    }
}

From source file:net.dv8tion.jda.entities.impl.MessageImpl.java

@Override
public String getStrippedContent() {
    if (strippedContent == null) {
        String tmp = getContent();
        //all the formatting keys to keep track of
        String[] keys = new String[] { "*", "_", "`", "~~" };

        //find all tokens (formatting strings described above)
        TreeSet<FormatToken> tokens = new TreeSet<>((t1, t2) -> Integer.compare(t1.start, t2.start));
        for (String key : keys) {
            Matcher matcher = Pattern.compile(Pattern.quote(key)).matcher(tmp);
            while (matcher.find()) {
                tokens.add(new FormatToken(key, matcher.start()));
            }/*  w w w .  ja v a  2s.c  o m*/
        }

        //iterate over all tokens, find all matching pairs, and add them to the list toRemove
        Stack<FormatToken> stack = new Stack<>();
        List<FormatToken> toRemove = new ArrayList<>();
        boolean inBlock = false;
        for (FormatToken token : tokens) {
            if (stack.empty() || !stack.peek().format.equals(token.format)
                    || stack.peek().start + token.format.length() == token.start) {
                //we are at opening tag
                if (!inBlock) {
                    //we are outside of block -> handle normally
                    if (token.format.equals("`")) {
                        //block start... invalidate all previous tags
                        stack.clear();
                        inBlock = true;
                    }
                    stack.push(token);
                } else if (token.format.equals("`")) {
                    //we are inside of a block -> handle only block tag
                    stack.push(token);
                }
            } else if (!stack.empty()) {
                //we found a matching close-tag
                toRemove.add(stack.pop());
                toRemove.add(token);
                if (token.format.equals("`") && stack.empty()) {
                    //close tag closed the block
                    inBlock = false;
                }
            }
        }

        //sort tags to remove by their start-index and iteratively build the remaining string
        Collections.sort(toRemove, (t1, t2) -> Integer.compare(t1.start, t2.start));
        StringBuilder out = new StringBuilder();
        int currIndex = 0;
        for (FormatToken formatToken : toRemove) {
            if (currIndex < formatToken.start) {
                out.append(tmp.substring(currIndex, formatToken.start));
            }
            currIndex = formatToken.start + formatToken.format.length();
        }
        if (currIndex < tmp.length()) {
            out.append(tmp.substring(currIndex));
        }
        //return the stripped text, escape all remaining formatting characters (did not have matching open/close before or were left/right of block
        strippedContent = out.toString().replace("*", "\\*").replace("_", "\\_").replace("~", "\\~");
    }
    return strippedContent;
}

From source file:qhindex.controller.SearchAuthorWorksController.java

private String removeNonLetterCharsAtBeginning(String string) {
    int indexFirstLetter = 0;
    Pattern letterOrNumberCharPatter = Pattern.compile("[a-zA-Z1-9]");
    Matcher matcher = letterOrNumberCharPatter.matcher(string);
    if (matcher.find(0)) {
        indexFirstLetter = matcher.start();
    }/*from  w ww  .  j  a  va  2s .  c om*/
    return string.substring(indexFirstLetter);
}

From source file:be.makercafe.apps.makerbench.editors.JFXMillEditor.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  ww w  .ja  v  a 2 s  .c o m
    }
    spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
    return spansBuilder.create();
}

From source file:com.github.braully.graph.BatchExecuteOperation.java

int indexOf(String str, String patern) {
    int ret = 0;//from   w w  w.  j  ava2 s  .  c o  m
    try {
        Pattern pattern = Pattern.compile(patern);
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {
            ret = matcher.start();
            //                System.out.println(matcher.start());//this will give you index
        }
    } catch (Exception e) {

    }
    return ret;
}

From source file:com.gargoylesoftware.htmlunit.javascript.regexp.HtmlUnitRegExpProxy.java

private String doReplacement(final String originalString, final String replacement, final Matcher matcher,
        final boolean replaceAll) {

    final StringBuilder sb = new StringBuilder();
    int previousIndex = 0;
    while (matcher.find()) {
        sb.append(originalString, previousIndex, matcher.start());
        String localReplacement = replacement;
        if (replacement.contains("$")) {
            localReplacement = computeReplacementValue(replacement, originalString, matcher);
        }/* w w  w .j a v a  2 s.com*/
        sb.append(localReplacement);
        previousIndex = matcher.end();

        setProperties(matcher, originalString, matcher.start(), previousIndex);
        if (!replaceAll) {
            break;
        }
    }
    sb.append(originalString, previousIndex, originalString.length());
    return sb.toString();
}

From source file:com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck.java

/**
 * Finds out what group the specified import belongs to.
 *
 * @param name the import name to find.//from w  ww. ja v  a 2  s . co  m
 * @return group number for given import name.
 */
private int getGroupNumber(String name) {
    int bestIndex = groups.length;
    int bestLength = -1;
    int bestPos = 0;

    // find out what group this belongs in
    // loop over groups and get index
    for (int i = 0; i < groups.length; i++) {
        final Matcher matcher = groups[i].matcher(name);
        while (matcher.find()) {
            final int length = matcher.end() - matcher.start();
            if (length > bestLength || length == bestLength && matcher.start() < bestPos) {
                bestIndex = i;
                bestLength = length;
                bestPos = matcher.start();
            }
        }
    }

    return bestIndex;
}

From source file:com.centurylink.mdw.hub.servlet.SoapServlet.java

protected String substituteRuntimeWsdl(String wsdl) {
    StringBuffer substituted = new StringBuffer(wsdl.length());
    Matcher matcher = tokenPattern.matcher(wsdl);
    int index = 0;
    while (matcher.find()) {
        String match = matcher.group();
        substituted.append(wsdl.substring(index, matcher.start()));
        String propName = match.substring(2, match.length() - 1);
        String value = PropertyManager.getProperty(propName);
        if (value != null)
            substituted.append(value);/*from  w ww. java 2 s . c  o m*/
        index = matcher.end();
    }
    substituted.append(wsdl.substring(index));
    return substituted.toString();
}

From source file:com.joliciel.talismane.sentenceDetector.SentenceDetectorImpl.java

@Override
public List<Integer> detectSentences(String prevText, String text, String moreText) {
    MONITOR.startTask("detectSentences");
    try {/*from   ww  w . j a  v  a  2  s  .c  o  m*/
        String context = prevText + text + moreText;

        // we only want one placeholder per start index - the first one that gets added
        Map<Integer, TokenPlaceholder> placeholderMap = new HashMap<Integer, TokenPlaceholder>();
        for (TokenFilter filter : this.preTokeniserFilters) {
            Set<TokenPlaceholder> myPlaceholders = filter.apply(context);
            for (TokenPlaceholder placeholder : myPlaceholders) {
                if (!placeholderMap.containsKey(placeholder.getStartIndex())) {
                    placeholderMap.put(placeholder.getStartIndex(), placeholder);
                }
            }
        }

        Matcher matcher = SentenceDetector.POSSIBLE_BOUNDARIES.matcher(text);
        Set<Integer> possibleBoundaries = new HashSet<Integer>();
        List<Integer> guessedBoundaries = new ArrayList<Integer>();

        while (matcher.find()) {
            // only add possible boundaries if they're not inside a placeholder
            boolean inPlaceholder = false;
            int position = prevText.length() + matcher.start();
            for (TokenPlaceholder placeholder : placeholderMap.values()) {
                if (placeholder.getStartIndex() <= position && position < placeholder.getEndIndex()) {
                    inPlaceholder = true;
                    break;
                }
            }
            if (!inPlaceholder)
                possibleBoundaries.add(position);
        }

        for (int possibleBoundary : possibleBoundaries) {
            PossibleSentenceBoundary boundary = this.sentenceDetectorService
                    .getPossibleSentenceBoundary(context, possibleBoundary);
            if (LOG.isTraceEnabled()) {
                LOG.trace("Testing boundary: " + boundary);
                LOG.trace(" at position: " + possibleBoundary);
            }

            List<FeatureResult<?>> featureResults = new ArrayList<FeatureResult<?>>();
            for (SentenceDetectorFeature<?> feature : features) {
                RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                FeatureResult<?> featureResult = feature.check(boundary, env);
                if (featureResult != null)
                    featureResults.add(featureResult);
            }
            if (LOG.isTraceEnabled()) {
                for (FeatureResult<?> result : featureResults) {
                    LOG.trace(result.toString());
                }
            }

            List<Decision<SentenceDetectorOutcome>> decisions = this.decisionMaker.decide(featureResults);
            if (LOG.isTraceEnabled()) {
                for (Decision<SentenceDetectorOutcome> decision : decisions) {
                    LOG.trace(decision.getCode() + ": " + decision.getProbability());
                }
            }

            if (decisions.get(0).getOutcome().equals(SentenceDetectorOutcome.IS_BOUNDARY)) {
                guessedBoundaries.add(possibleBoundary - prevText.length());
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Adding boundary: " + possibleBoundary);
                }
            }
        } // have we a possible boundary at this position?

        return guessedBoundaries;
    } finally {
        MONITOR.endTask("detectSentences");
    }
}