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(String name) 

Source Link

Document

Returns the start index of the subsequence captured by the given named-capturing group during the previous match operation.

Usage

From source file:com.puppycrawl.tools.checkstyle.checks.javadoc.WriteTagCheck.java

/**
 * Verifies that a type definition has a required tag.
 * @param lineNo the line number for the type definition.
 * @param comment the Javadoc comment for the type definition.
 *//*from w w  w  . ja v a 2 s. co  m*/
private void checkTag(int lineNo, String... comment) {
    if (tagRE == null) {
        return;
    }

    int tagCount = 0;
    for (int i = 0; i < comment.length; i++) {
        final String commentValue = comment[i];
        final Matcher matcher = tagRE.matcher(commentValue);
        if (matcher.find()) {
            tagCount += 1;
            final int contentStart = matcher.start(1);
            final String content = commentValue.substring(contentStart);
            if (tagFormatRE == null || tagFormatRE.matcher(content).find()) {
                logTag(lineNo + i - comment.length, tag, content);
            } else {
                log(lineNo + i - comment.length, TAG_FORMAT, tag, tagFormat);
            }
        }
    }
    if (tagCount == 0) {
        log(lineNo, MISSING_TAG, tag);
    }

}

From source file:com.google.testing.pogen.parser.template.RegexVariableExtractor.java

private String processCharacters() {
    String text = lastText;/*from w w  w.ja v a  2  s .c  o  m*/
    Matcher matcher = variablePattern.matcher(lastText);
    while (matcher.find()) {
        if (excludedRanges.contains(matcher.start(1))) {
            continue;
        }
        // tagInfoStack always has some elements
        // because NekoHTML add <html> tag as a root element automatically
        // Note that tags automatically added have -1 start/end indices
        HtmlTagInfo tagInfo = tagInfoStack.peek();
        int iGroup = getFirstAvailableGroupIndex(matcher);
        tagInfo.addVariableInfo(matcher.group(0), matcher.group(iGroup), matcher.start(iGroup));
    }
    lastText = "";
    return text;
}

From source file:net.erdfelt.android.sdkfido.util.PropertyExpander.java

public String expand(String str) {
    if (StringUtils.isEmpty(str)) {
        // Empty string. Fail fast.
        return str;
    }/*from w w  w.j a v  a 2s . c  om*/

    if (str.indexOf("@") < 0) {
        // Contains no potential expressions. Fail fast.
        return str;
    }

    Matcher mat = pat.matcher(str);
    int offset = 0;
    String expression;
    String value;
    StringBuffer expanded = new StringBuffer();

    while (mat.find(offset)) {
        expression = mat.group(1);

        expanded.append(str.substring(offset, mat.start(1)));
        value = props.get(mat.group(2));
        if (value != null) {
            expanded.append(value);
        } else {
            expanded.append(expression);
        }
        offset = mat.end(1);
    }

    expanded.append(str.substring(offset));

    if (expanded.indexOf("@@") >= 0) {
        // Special case for escaped content.
        return expanded.toString().replaceAll("\\@\\@", "\\@");
    } else {
        // return expanded
        return expanded.toString();
    }
}

From source file:com.norconex.commons.lang.io.TextReader.java

/**
 * Reads the next chunk of text, up to the maximum read size specified.
 * It tries as much as possible to break long text into paragraph,
 * sentences or words, before returning.  See class documentation.
 * @return text read/*from w w  w.j a v  a2 s  .  co  m*/
 * @throws IOException problem reading text.
 */
public String readText() throws IOException {
    char[] text = new char[maxReadSize - buffer.length()];
    int num = reader.read(text);
    if (num == -1) {
        return null;
    }

    buffer.append(String.valueOf(text, 0, num));

    // Return all if we reached the end.
    reader.mark(1);
    if (reader.read() == -1) {
        String t = buffer.toString();
        buffer.setLength(0);
        reader.reset();
        return t;
    } else {
        reader.reset();
    }

    Matcher m = null;

    // Try breaking at paragraph:
    m = paragraphDelimiterPattern.matcher(buffer);
    if (m.find()) {
        int mStart = m.start(m.groupCount());
        int mEnd = m.end(m.groupCount());
        int substringEnd = mEnd;
        if (removeTrailingDelimiter) {
            substringEnd = mStart;
        }
        String t = buffer.substring(0, substringEnd);
        buffer.delete(0, substringEnd);
        return t;
    }

    // Try breaking at sentence:
    m = sentencePattern.matcher(buffer);
    if (m.find()) {
        int mStart = m.start(1);
        int mEnd = m.end(1);
        int substringEnd = mEnd;
        if (removeTrailingDelimiter) {
            substringEnd = mStart;
        }
        String t = buffer.substring(0, substringEnd);
        buffer.delete(0, substringEnd);
        return t;
    }

    // Try breaking at word:
    m = wordDelimiterPattern.matcher(buffer);
    if (m.find()) {
        int mStart = m.start(m.groupCount());
        int mEnd = m.end(m.groupCount());
        int substringEnd = mEnd;
        if (removeTrailingDelimiter) {
            substringEnd = mStart;
        }
        String t = buffer.substring(0, substringEnd);
        buffer.delete(0, substringEnd);
        return t;
    }

    String t = buffer.toString();
    buffer.setLength(0);
    return t;
}

From source file:cn.dreampie.resource.LessSource.java

private String includeImportedContent(LessSource importedLessSource, Matcher importMatcher) {
    StringBuilder builder = new StringBuilder();
    builder.append(normalizedContent.substring(0, importMatcher.start(1)));

    String mediaQuery = importMatcher.group(8);
    if (mediaQuery != null && mediaQuery.length() > 0) {
        builder.append("@media");
        builder.append(mediaQuery);//from w  w w. j  a  v a 2  s.  c o  m
        builder.append("{\n");
    }
    builder.append(importedLessSource.getNormalizedContent());
    if (mediaQuery != null && mediaQuery.length() > 0) {
        builder.append("}\n");
    }
    builder.append(normalizedContent.substring(importMatcher.end(1)));
    return builder.toString();
}

From source file:de.tudarmstadt.ukp.dkpro.core.textnormalizer.ReplacementFileNormalizer.java

@Override
protected Map<Integer, List<SofaChangeAnnotation>> createSofaChangesMap(JCas jcas) {
    Map<Integer, List<SofaChangeAnnotation>> changesMap = new TreeMap<Integer, List<SofaChangeAnnotation>>();
    int mapKey = 1;

    String coveredText = jcas.getDocumentText().toLowerCase();

    List<SofaChangeAnnotation> scaChangesList = new ArrayList<SofaChangeAnnotation>();
    for (Map.Entry<String, String> entry : replacementMap.entrySet()) {
        String replacementKey = entry.getKey().toLowerCase();
        String replacementValue = targetSurroundings + entry.getValue() + targetSurroundings;

        String regex = srcSurroundingsStart + "(" + Pattern.quote(replacementKey) + ")" + srcSurroundingsEnd;
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(coveredText);

        int groupNumberOfKey = (matcher.groupCount() == 1) ? 1 : 2;

        while (matcher.find()) {
            int start = matcher.start(groupNumberOfKey);
            int end = matcher.end(groupNumberOfKey);

            SofaChangeAnnotation sca = new SofaChangeAnnotation(jcas);
            sca.setBegin(start);//from w  ww  . j a  va2  s . c  o m
            sca.setEnd(end);
            sca.setOperation(OP_REPLACE);
            sca.setValue(replacementValue);
            scaChangesList.add(sca);

            System.out.println(matcher.group(0));
        }

    }
    changesMap.put(mapKey++, scaChangesList);

    return changesMap;
}

From source file:com.edgenius.wiki.render.filter.LinkFilter.java

public List<Region> getRegions(CharSequence input) {
    final List<Region> list = new ArrayList<Region>();
    regexProvider.replaceByTokenVisitor(input, new TokenVisitor<Matcher>() {
        public void handleMatch(StringBuffer buffer, Matcher matcher) {

            int contentStart = matcher.start(1);
            int contentEnd = matcher.end(1);
            int start = contentStart - 1;
            int end = contentEnd + 1;

            String full = matcher.group(1);
            int sep;
            //link has possible 2 Region, [view>link], entire text is immutable region, but view is mutable. 
            if ((sep = StringUtil.indexSeparatorWithoutEscaped(full, ">")) != -1) {
                //entire is immutable
                Region bodyRegion = new Region(LinkFilter.this, true, start, end, contentStart, contentEnd);

                //view part is normal mutable, it needs independent render 
                Region viewPartRegion = new Region(LinkFilter.this, false, contentStart, contentStart + sep,
                        contentStart, contentStart + sep);
                bodyRegion.setSubRegion(viewPartRegion);

                list.add(bodyRegion);//from ww w  .  ja  va 2s.  c o m
            } else {
                //[viewAsLink] only 1 region, and it is immutable 
                list.add(new Region(LinkFilter.this, true, start, end, contentStart, contentEnd));
            }
        }

    });
    return list;
}

From source file:ch.sourcepond.maven.release.pom.VersionTransferWriter.java

@Override
public void close() throws IOException {
    final Matcher matcher = VERSION_PATTERN.matcher(toString());
    final Matcher originalMatcher = VERSION_PATTERN.matcher(original);
    int originalIdx = 0;
    int startIdx = 0;

    while (find(matcher, originalMatcher, originalIdx)) {
        final String newVersion = matcher.group(VERSION_VALUE);
        startIdx = originalMatcher.start(VERSION_VALUE);
        original.replace(startIdx, originalMatcher.end(VERSION_VALUE), newVersion);
        originalIdx = startIdx + newVersion.length();
    }//w w  w .j  a v a2 s .  c om

    try (final Writer writer = new BufferedWriter(new FileWriter(file))) {
        writer.write(original.toString());
    }
}

From source file:fr.smile.liferay.LiferayUrlRewriter.java

/**
 * Fix all resources urls and return the result.
 *
 * @param input        The original charSequence to be processed.
 * @param requestUrl   The request URL.//w ww .  j  av a 2s  . c  om
 * @param baseUrlParam The base URL selected for this request.
 * @return the result of this renderer.
 */
public CharSequence rewriteHtml(CharSequence input, String requestUrl, Pattern pattern, String baseUrlParam,
        String visibleBaseUrl) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("input=" + input);
        LOG.debug("rewriteHtml (requestUrl=" + requestUrl + ", pattern=" + pattern + ",baseUrlParam)"
                + baseUrlParam + ",strVisibleBaseUrl=" + visibleBaseUrl + ")");
    }

    StringBuffer result = new StringBuffer(input.length());
    Matcher m = pattern.matcher(input);
    while (m.find()) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("found match: " + m);
        }
        String url = input.subSequence(m.start(3) + 1, m.end(3) - 1).toString();
        url = rewriteUrl(url, requestUrl, baseUrlParam, visibleBaseUrl);
        url = url.replaceAll("\\$", "\\\\\\$"); // replace '$' -> '\$' as it
        // denotes group
        StringBuffer tagReplacement = new StringBuffer("<$1$2=\"").append(url).append("\"");
        if (m.groupCount() > 3) {
            tagReplacement.append("$4");
        }
        tagReplacement.append('>');
        if (LOG.isTraceEnabled()) {
            LOG.trace("replacement: " + tagReplacement);
        }
        m.appendReplacement(result, tagReplacement.toString());
    }
    m.appendTail(result);

    return result;
}

From source file:fr.eurecom.nerd.core.proxy.ExtractivClient.java

private List<TEntity> parse(String text, String serviceKey, OntologyType otype) {
    List<TEntity> result = new LinkedList<TEntity>();
    URI endpoint;/*from w w  w  .  j  a  v  a  2 s.c  o m*/
    try {
        endpoint = new URI(EXTRACTIV_SERVER_LOCATION);
        HttpMethodBase extractivRequest = getExtractivProcessString(endpoint, text, serviceKey);
        InputStream extractivResults = fetchHttpRequest(extractivRequest);
        Readable jsonReadable = new InputStreamReader(extractivResults);
        ExtractivJSONParser jsonParser = new ExtractivJSONParser(jsonReadable);

        Map<String, Integer> map = new HashMap<String, Integer>();
        for (Document document : jsonParser)
            for (com.extractiv.Entity item : document.getEntities()) {
                String label = item.asString();
                String type = item.getType();
                String nerdType = OntoFactory.mapper.getNerdType(otype, label, SOURCE, type).toString();
                String uri = (item.getLinks().size() > 0) ? item.getLinks().get(0) : "null";
                //                    Integer startChar = item.getOffset();
                //                    Integer endChar = startChar + item.getCharLength();
                //                    TEntity extraction = new TEntity(label, type, uri, nerdType, 
                //                    startChar, endChar, confidence, SOURCE); 
                //                    result.add(extraction);

                //logic to compute the startchar and endchar of the entity within the text
                Integer startchar = null, endchar = null;
                if (map.containsKey(label)) {
                    int value = map.get(label);
                    map.remove(label);
                    map.put(label, new Integer(value + 1));
                } else
                    map.put(label, new Integer(1));

                try {
                    Pattern p = Pattern.compile("\\b" + label + "\\b");
                    Matcher m = p.matcher(text);
                    for (int j = 0; j < map.get(label) && m.find(); j++) {
                        startchar = m.start(0);
                        endchar = m.end(0);
                        if (containsAtIndex(result, startchar, endchar))
                            j--;
                    }

                    Double confidence = 0.5;

                    if (startchar != null && endchar != null) {
                        TEntity extraction = new TEntity(label, type, uri, nerdType.toString(), startchar,
                                endchar, confidence, SOURCE);

                        result.add(extraction);
                    }
                } catch (PatternSyntaxException eregex) {
                    eregex.printStackTrace();
                }
            }
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (BadInputException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    return result;
}