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.igormaznitsa.jcp.utils.PreprocessorUtils.java

@Nonnull
public static String processMacroses(@Nonnull final String processingString,
        @Nonnull final PreprocessorContext context) {
    int position;
    String result = processingString;

    if (context.isAllowWhitespace()) {
        final Matcher matcher = PATTERN_MACROS_WITH_SPACES.matcher(processingString);
        final StringBuilder buffer = new StringBuilder();
        int end = 0;
        while (matcher.find()) {
            final int start = matcher.start();
            final int prevEnd = end;
            end = matcher.end();
            final String macrosBody = matcher.group(1);
            final Value value = Expression.evalExpression(macrosBody, context);
            buffer.append(processingString.substring(prevEnd, start));
            buffer.append(value.toString());
        }//from   w  w  w  .j a  v  a2s  .co m
        if (end < processingString.length()) {
            buffer.append(processingString.substring(end));
        }
        result = buffer.toString();
    } else {
        while (true) {
            position = result.indexOf("/*$");

            if (position >= 0) {
                final String leftPart = result.substring(0, position);
                final int beginIndex = position;
                position = result.indexOf("$*/", position);
                if (position >= 0) {
                    final String macrosBody = result.substring(beginIndex + 3, position);
                    final String rightPart = result.substring(position + 3);

                    final Value value = Expression.evalExpression(macrosBody, context);

                    result = leftPart + value.toString() + rightPart;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
    }
    return result;
}

From source file:com.bjond.utilities.MiscUtils.java

/**
* Given an original string this method will normalize all numerics held within
 * that string (if any) to allow for Natural Sort Ordering.
 *
 *  https://en.wikipedia.org/wiki/Natural_sort_order
 *
 *  Algorithm:/*from w w  w  . j  av a 2 s  .  co  m*/
 *  Our approach basically matches a precompiled regular expression against 
 *  a string and extracs all numeric substrings. This is standard and fast 
 *  regex concept that actually has a special match character: \d+
 *
 *  For each numeric normailze it and construct a new string with the normalized
 *  numeric in place of the original.
 *  
 *  Normalization in this instance is that each numeric contain the same number of
 *  places: 75. Each numeric less than 75 will be prepended with zeros. 
 *
 *  This algorithm can also be thought of as a mapping in which a numeric is
 *  mapped to another numeric that always contains 75 places.
 *
 *  Any numeric greater than 75 results in a bad order and a warning is emitted. 
 *  Nothing can be done beyond increasing the normailation places. Highly unlikely.
* 
 *  Implementation Notes:
 *  Emphasis on performance thus the code is a bit more complex than you would expect.
 *  I could make it even more complex and more performant (theoretically) by reducing
 *  temporary objects further but the resulting code would be exceedingly complex and 
 *  error prone. A balancing of the two requirements of maintainability and performance
 *  were considered.
 * 
* @param original
* @return
*/
public static String normalizeToNaturalSortOrder(final String original) {
    // Guard
    if (StringUtils.isBlank(original)) {
        return "";
    }

    // The normalized size of a numeric. 75 places
    final int NORMALIZED = 75;

    // Match on all numerics
    final Matcher m = numericPattern.matcher(original);

    // Flip through all numerics, if any, and normalize
    final StringBuilder sb = new StringBuilder(500);
    int index = 0; // Current location in string.
    while (m.find()) {
        final String numeric = original.substring(m.start(), m.end());

        // First insert any previous characters.
        sb.append(original.substring(index, m.start()));
        index = m.end();

        final int zeros = NORMALIZED - numeric.length();
        if (zeros > 0) { // if length > NORMALIZED we blew the sort.
            for (int i = 0; i < zeros; sb.append("0"), i++)
                ;
        } else {
            log.warn("Normalized numeric is greater than {} places. {}", NORMALIZED, original);
        }
        sb.append(numeric);
    }

    // Append anything non-numeric for the remainder of the string
    // if any.
    if (index < original.length()) {
        sb.append(original.substring(index));
    }

    return sb.toString();
}

From source file:com.jaspersoft.jasperserver.api.engine.common.service.impl.ActionModel.java

private static String generateOptionLabel(ActionModelSupport actionModelContext, String labelExpression,
        String optionValue, String optionId) {
    //first substitute option value (assumes just one of these)
    labelExpression = labelExpression.replace(RES_OPTION_VALUE_SERVER, optionValue);
    labelExpression = labelExpression.replace(RES_OPTION_ID_SERVER, optionId);
    // now look for $R{...} pattern and resolve i18n value (assumes just one of these)
    Matcher matcher = RegexUtil.getResourceKeyPattern(resourceKeyRegularExpression).matcher(labelExpression);
    if (matcher.find()) {
        String resourceKeyPattern = labelExpression.substring(matcher.start(), matcher.end());
        String resourceKey = resourceKeyPattern.substring(3, resourceKeyPattern.length() - 1);
        String translation = getLocalizedValue(resourceKey, actionModelContext);
        return labelExpression.replace(resourceKeyPattern, translation);
    } else {//from   w ww.jav a2  s.c  o  m
        return labelExpression;
    }
}

From source file:com.github.gekoh.yagen.hibernate.PatchHibernateMappingClasses.java

public static Collection<String> splitSQL(String sql) {
    Matcher matcher = SEPARATOR_PATTERN.matcher(sql);
    int endIdx, idx = 0;
    ArrayList<String> statements = new ArrayList<String>();

    while (matcher.find(idx)) {
        endIdx = matcher.start();//from w  w w .j av  a  2 s.  c om

        if (endIdx - idx > 0) {
            statements.add(sql.substring(idx, endIdx));
        }

        if (endIdx >= 0) {
            idx = matcher.end();
        }
    }

    if (idx < sql.length()) {
        String singleSql = sql.substring(idx);
        if (StringUtils.isNotEmpty(singleSql.trim())) {
            statements.add(singleSql);
        }
    }

    for (int i = 0; i < statements.size(); i++) {
        String stmt = statements.get(i);
        if (stmt == null || stmt.trim().length() < 1) {
            statements.remove(i);
            i--;
            continue;
        }
        matcher = COMMENT_PATTERN.matcher(stmt);
        if (matcher.find() && stmt.substring(0, matcher.start()).trim().length() < 1) {
            statements.remove(i);
            statements.add(i, stmt.substring(matcher.end()));
            if (stmt.substring(0, matcher.end()).trim().length() > 0) {
                statements.add(i, stmt.substring(0, matcher.end()));
            }
        }
    }

    return statements;
}

From source file:com.twosigma.beaker.groovy.evaluator.GroovyEvaluator.java

static String envVariablesFilter(String p, Map<String, String> env) {

    if (p == null)
        return p;

    for (Pattern pattern : envVariablePatterns) {

        Matcher matcher = pattern.matcher(p);

        String r = "";

        int lastIndex = 0;

        while (matcher.find()) {

            String var = matcher.group(1);

            String substitute = env.get(var);

            if (substitute == null)
                substitute = "";

            r += p.substring(lastIndex, matcher.start());

            r += substitute;//from   w w w  .  j  av a 2s  . c  o  m

            lastIndex = matcher.end();
        }

        r += p.substring(lastIndex, p.length());

        p = r;

    }

    return p;
}

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

/** Tries to find better matching regular expression:
 * longer matching substring wins; in case of the same length,
 * lower position of matching substring wins.
 * @param importPath/*from   w  ww  .j  a  v a2 s.  c  o m*/
 *      Full import identifier
 * @param group
 *      Import group we are trying to assign the import
 * @param regExp
 *      Regular expression for import group
 * @param currentBestMatch
 *      object with currently best match
 * @return better match (if found) or the same (currentBestMatch)
 */
private static RuleMatchForImport findBetterPatternMatch(String importPath, String group, Pattern regExp,
        RuleMatchForImport currentBestMatch) {
    RuleMatchForImport betterMatchCandidate = currentBestMatch;
    final Matcher matcher = regExp.matcher(importPath);
    while (matcher.find()) {
        final int length = matcher.end() - matcher.start();
        if (length > betterMatchCandidate.matchLength || length == betterMatchCandidate.matchLength
                && matcher.start() < betterMatchCandidate.matchPosition) {
            betterMatchCandidate = new RuleMatchForImport(group, length, matcher.start());
        }
    }
    return betterMatchCandidate;
}

From source file:Main.java

/**
 * Converts a given message to CTCP formatting.
 *
 * @param message message to convert/*from w w w .j a  v  a2  s. c om*/
 * @return converted message
 */
static String toCTCP(String message) {
    StringBuilder builder = new StringBuilder();
    builder.append(CTCP_DELIMITER);
    int currentIndex = 0;
    Matcher matcher = CTCP_ESCAPABLE_CHAR.matcher(message);
    while (matcher.find()) {
        if (matcher.start() > currentIndex) {
            builder.append(message.substring(currentIndex, matcher.start()));
        }
        switch (matcher.group()) {
        case "\n":
            builder.append(CTCP_MQUOTE).append('n');
            break;
        case "\r":
            builder.append(CTCP_MQUOTE).append('r');
            break;
        case "\u0000":
            builder.append(CTCP_MQUOTE).append('0');
            break;
        case CTCP_MQUOTE + "":
            builder.append(CTCP_MQUOTE).append(CTCP_MQUOTE);
            break;
        case CTCP_DELIMITER + "":
            builder.append("\\a");
            break;
        case "\\":
            builder.append("\\\\");
            break;
        }
        currentIndex = matcher.end();
    }
    if (currentIndex < message.length()) {
        builder.append(message.substring(currentIndex));
    }
    builder.append(CTCP_DELIMITER);
    return builder.toString();
}

From source file:io.github.lucaseasedup.logit.config.PredefinedConfiguration.java

/**
 * Converts a hyphenated path (example-path.secret-setting)
 * to a camelCase path (examplePath.secretSetting).
 * /*w w w  .ja va  2s.com*/
 * @param hyphenatedPath the hyphenated path.
 * 
 * @return the camelCase equivalent of the provided hyphenated path.
 */
public static String getCamelCasePath(String hyphenatedPath) {
    Matcher matcher = HYPHENATED_PATH_PATTERN.matcher(hyphenatedPath);
    StringBuilder sb = new StringBuilder();
    int last = 0;

    while (matcher.find()) {
        sb.append(hyphenatedPath.substring(last, matcher.start()));
        sb.append(matcher.group(1).toUpperCase());

        last = matcher.end();
    }

    sb.append(hyphenatedPath.substring(last));

    return sb.toString();
}

From source file:io.swagger.codegen.languages.AbstractSwift3Codegen.java

private static String normalizePath(String path) {
    StringBuilder builder = new StringBuilder();

    int cursor = 0;
    Matcher matcher = PATH_PARAM_PATTERN.matcher(path);
    boolean found = matcher.find();
    while (found) {
        String stringBeforeMatch = path.substring(cursor, matcher.start());
        builder.append(stringBeforeMatch);

        String group = matcher.group().substring(1, matcher.group().length() - 1);
        group = camelize(group, true);/*from w w w  .j  a  v a  2  s.  c  om*/
        builder.append("{").append(group).append("}");

        cursor = matcher.end();
        found = matcher.find();
    }

    String stringAfterMatch = path.substring(cursor);
    builder.append(stringAfterMatch);

    return builder.toString();
}

From source file:com.github.gekoh.yagen.hibernate.PatchHibernateMappingClasses.java

public static SqlStatement prepareDDL(String sql) {
    sql = sql.trim();/*from  w  w  w.j a v  a2  s  .  co  m*/
    String delimiter = "";

    Matcher matcher = PLSQL_END_PATTERN.matcher(sql);
    if (matcher.find()) {
        if (matcher.group(2) != null) {
            sql = sql.substring(0, matcher.start(2));
        }
        sql += "\n";
        delimiter = "/";
    }
    // remove trailing semicolon in case of non pl/sql type objects/statements
    else if (sql.endsWith(";")) {
        sql = sql.substring(0, sql.length() - 1);
    }

    StringBuilder sqlWoComments = new StringBuilder(sql);
    while ((matcher = COMMENT_PATTERN.matcher(sqlWoComments.toString())).find()) {
        sqlWoComments.delete(matcher.start(), matcher.end());
    }

    if (delimiter.length() < 1 && sqlWoComments.toString().trim().length() > 0) {
        delimiter = ";";
    }

    return new SqlStatementImpl(sql, delimiter);
}