Example usage for java.util.regex Matcher end

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

Introduction

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

Prototype

public int end() 

Source Link

Document

Returns the offset after the last character matched.

Usage

From source file:com.jkoolcloud.tnt4j.streams.utils.Range.java

/**
 * Parses string defining range values. Range definition separator is '{@value #RANGE_SEPARATOR}'.
 * /*from w ww .j a  va 2s  . c  om*/
 * @param rangeStr
 *            range definition string to parse
 * @param pattern
 *            RegEx pattern to use
 * @return string array where first element defines range lower bound and second element defines upper bound
 * @throws Exception
 *             if range string can't be parsed
 */
protected static String[] parseRange(String rangeStr, Pattern pattern) throws Exception {
    String cs = StringUtils.trimToNull(rangeStr);
    if (StringUtils.isEmpty(cs)) {
        throw new IllegalArgumentException(
                StreamsResources.getString(StreamsResources.RESOURCE_BUNDLE_NAME, "Range.range.string.empty"));
    }
    int rCharIdx = cs.indexOf(RANGE_SEPARATOR);

    String[] numStrs = new String[2];
    int si = 0;
    Matcher m = pattern.matcher(cs);
    while (m.find()) {
        String g = m.group();
        if (StringUtils.isNotEmpty(g)) {
            numStrs[si++] = g;
        }
        m.end();
    }

    String fromStr = null;
    String toStr = null;

    if (rCharIdx == -1) { // no range separator symbol found - unary range
        fromStr = numStrs.length > 0 ? numStrs[0] : null;
        toStr = fromStr;
    } else {
        if (rCharIdx == 0) { // unbound low range
            toStr = numStrs.length > 0 ? numStrs[0] : null;
        } else if (rCharIdx == cs.length()) { // unbound high range
            fromStr = numStrs.length > 0 ? numStrs[0] : null;
        } else { // bounded range
            fromStr = numStrs.length > 0 ? numStrs[0] : null;
            toStr = numStrs.length > 1 ? numStrs[1] : null;
        }
    }

    numStrs[0] = fromStr;
    numStrs[1] = toStr;

    return numStrs;
}

From source file:com.mirth.connect.util.CodeTemplateUtil.java

public static String stripDocumentation(String code) {
    if (StringUtils.isNotBlank(code)) {
        code = StringUtils.trim(code);/* ww  w  .ja va 2  s.  c  om*/
        Matcher matcher = COMMENT_PATTERN.matcher(code);
        if (matcher.find()) {
            return StringUtils.trim(code.substring(matcher.end()));
        }
    }

    return code;
}

From source file:Main.java

public static String escapeXPathField(String field) {
    Matcher matcher = Pattern.compile("['\"]").matcher(field);
    StringBuilder buffer = new StringBuilder("concat(");
    int start = 0;
    while (matcher.find()) {
        buffer.append("'").append(field.substring(start, matcher.start())).append("',");
        buffer.append("'".equals(matcher.group()) ? "\"'\"," : "'\"',");
        start = matcher.end();
    }//from  w w w .j a va  2 s .com
    if (start == 0)
        return "'" + field + "'";
    return buffer.append("'").append(field.substring(start)).append("'").append(")").toString();
}

From source file:com.mirth.connect.util.CodeTemplateUtil.java

public static String updateCode(String code) {
    if (StringUtils.isNotBlank(code)) {
        code = StringUtils.trim(code);//from ww w.  jav  a 2  s.  c o  m
        int endIndex = 0;
        Matcher matcher = COMMENT_PATTERN.matcher(code);
        if (matcher.find()) {
            endIndex = matcher.end();
        }

        CodeTemplateDocumentation documentation = getDocumentation(code);
        String description = documentation.getDescription();
        CodeTemplateFunctionDefinition functionDefinition = documentation.getFunctionDefinition();

        if (StringUtils.isBlank(description)) {
            description = "Modify the description here. Modify the function name and parameters as needed. One function per template is recommended; create a new code template for each new function.";
        }

        StringBuilder builder = new StringBuilder("/**");

        for (String descriptionLine : description.split("\r\n|\r|\n")) {
            builder.append("\n\t").append(WordUtils.wrap(descriptionLine, 100, "\n\t", false));
        }

        if (functionDefinition != null) {
            builder.append('\n');

            if (CollectionUtils.isNotEmpty(functionDefinition.getParameters())) {
                for (Parameter parameter : functionDefinition.getParameters()) {
                    StringBuilder parameterBuilder = new StringBuilder("\n\t@param {");
                    parameterBuilder.append(StringUtils.defaultString(parameter.getType(), "Any"));
                    parameterBuilder.append("} ").append(parameter.getName()).append(" - ")
                            .append(StringUtils.trimToEmpty(parameter.getDescription()));

                    builder.append(WordUtils.wrap(parameterBuilder.toString(), 100, "\n\t\t", false));
                }
            }

            StringBuilder returnBuilder = new StringBuilder("\n\t@return {")
                    .append(StringUtils.defaultString(functionDefinition.getReturnType(), "Any")).append("} ")
                    .append(StringUtils.trimToEmpty(functionDefinition.getReturnDescription()));

            builder.append(WordUtils.wrap(returnBuilder.toString(), 100, "\n\t\t", false));
        }

        builder.append("\n*/\n");

        return builder.toString() + code.substring(endIndex);
    }

    return code;
}

From source file:disko.ParagraphDetector.java

public static void detectParagraphs(String s, Collection<Ann> annotations) {
    Matcher m = paragraphSplitter.matcher(s);
    int start = 0;
    while (m.find()) {
        int end = m.start();
        String found = s.subSequence(start, end).toString();
        ParagraphAnn ann = new ParagraphAnn(start, end, found);
        annotations.add(ann);//from  w  ww.j  a v a  2  s .  c  o m
        log.debug("Found ParagraphAnn " + ann);
        start = m.end();
    }
    if (annotations.isEmpty() && s.trim().length() > 0)
        annotations.add(new ParagraphAnn(0, s.length(), s));
}

From source file:com.google.code.maven.plugin.http.client.utils.HttpEntityUtils.java

public static Charset getEncoding(HttpEntity entity, Charset defaultCharset, Log log) {
    try {//from  ww w.j a va2s .  c o m
        if (entity.getContentEncoding() != null) {
            return Charset.forName(entity.getContentEncoding().getName());
        } else if (entity.getContentType() != null) {
            String type = entity.getContentType().getValue();
            if (type != null) {
                Matcher charsetMatcher = CONTENT_TYPE_CHARSET_PATTERN.matcher(type);
                if (charsetMatcher.find()) {
                    Matcher delimiterMatcher = CONTENT_TYPE_CHARSET_DELIMITER_PATTERN.matcher(type);
                    String charsetName = null;
                    if (delimiterMatcher.find(charsetMatcher.end())) {
                        charsetName = type.substring(charsetMatcher.end(), delimiterMatcher.start());
                    } else {
                        charsetName = type.substring(charsetMatcher.end());
                    }
                    return Charset.forName(charsetName);
                }

            }
        } else {
            log.warn("encoding not defined in content encoding nor in content type");
        }
    } catch (IllegalCharsetNameException icne) {
        log.warn("failed to determine response encoding", icne);
    } catch (UnsupportedCharsetException uce) {
        log.warn("failed to determine response encoding", uce);
    }
    log.warn("back to default platform encoding " + DEFAULT_PLATFORM_ENCODING);
    return defaultCharset;
}

From source file:com.android.dialer.lookup.whitepages.WhitePagesApi.java

private static String extractXmlRegex(String str, String regex, String tag) {
    Pattern p = Pattern.compile(regex, Pattern.DOTALL);
    Matcher m = p.matcher(str);
    if (m.find()) {
        return extractXmlTag(str, m.start(), m.end(), tag);
    }/*from ww  w. ja v a 2 s  .c  o  m*/
    return null;
}

From source file:org.openimaj.web.scraping.images.ImgurClient.java

/**
 * @param url//from w w w .  j ava  2  s  .c o m
 * @return the imgur type and hash, or null if the URL was too tricky
 */
public static ImgurTypeHash imgurURLtoHash(URL url) {
    if (!url.getHost().contains("imgur"))
        return null;
    final String path = url.getPath();
    final String[] split = path.split("[/]+");
    if (split.length == 0)
        return null;
    else if (split.length == 2) {
        if (split[1].equals("gallery"))
            return new ImgurTypeHash(ImgurType.GALLERY, null);
        else {
            final Matcher matcher = hashPattern.matcher(split[1]);
            if (matcher.find()) {
                final String hash = split[1].substring(0, matcher.end());
                return new ImgurTypeHash(ImgurType.IMAGE, hash);
            }
            return null;
        }
    } else {
        final String hashPart = split[split.length - 1];
        final String typePart = split[split.length - 2];
        ImgurType type = ImgurType.IMAGE;
        if (typePart.equals("a"))
            type = ImgurType.ALBUM;

        final Matcher matcher = hashPattern.matcher(hashPart);
        matcher.find();
        final String hash = hashPart.substring(0, matcher.end());
        return new ImgurTypeHash(type, hash);
    }

}

From source file:fr.gouv.culture.thesaurus.util.TextUtils.java

/**
 * Abbrge (dans la mesure du possible possible) le texte sans tronquer les
 * mots, en partant de la gauche.//from w  w  w.  j ava2  s. co m
 * 
 * @param texte
 *            Texte  abbrger (peut tre <code>null</code>)
 * @param maxWidth
 *            Longueur max de la chane en sortie
 * @return Chane abbrge, ou <code>null</code> si le texte en entre tait
 *         <code>null</code>
 * 
 * @see StringUtils#abbreviate(String, int, int)
 */
public static String leftAbbreviateOnWords(final String texte, final int maxWidth) {
    String abbreviatedVersion;

    if (maxWidth < ELLIPSIS.length()) {
        throw new IllegalArgumentException("Max length is insufficient.");
    }

    if (texte == null || texte.length() <= maxWidth) {
        abbreviatedVersion = texte;
    } else {
        String abbreviated = texte.substring(0, maxWidth - ELLIPSIS.length());
        Matcher matcher = NON_WORD_CHARACTER.matcher(abbreviated);
        int lastWordEndIndex = abbreviated.length();
        while (matcher.find()) {
            lastWordEndIndex = matcher.end();
        }
        abbreviatedVersion = abbreviated.substring(0, lastWordEndIndex) + ELLIPSIS;
    }

    return abbreviatedVersion;
}

From source file:eu.eubrazilcc.lvl.core.util.QueryUtils.java

public static ImmutableMap<String, String> parseQuery(final String query, final boolean deduplicate) {
    final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
    if (isNotBlank(query)) {
        String fullText = query;//from  w w  w . j  a  v  a2 s . co m
        final Matcher matcher = KEYWORD_PATTERN.matcher(query);
        while (matcher.find()) {
            final String[] keyword = extractKeyword(matcher.group());
            builder.put(keyword[0], remove(keyword[1], QUOTES));
            fullText = strikethrough(fullText, matcher.start(), matcher.end());
        }
        fullText = deduplicate ? normalize(fullText) : normalizeSpace(fullText);
        if (isNotBlank(fullText)) {
            builder.put(TEXT_FIELD, remove(fullText, QUOTES));
        }
    }
    return builder.build();
}