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.fsck.k9.helper.Utility.java

/**
 * Extract the 'original' subject value, by ignoring leading
 * response/forward marker and '[XX]' formatted tags (as many mailing-list
 * softwares do)./*from  ww w.  j  av  a  2s. c  o m*/
 *
 * <p>
 * Result is also trimmed.
 * </p>
 *
 * @param subject
 *            Never <code>null</code>.
 * @return Never <code>null</code>.
 */
public static String stripSubject(final String subject) {
    int lastPrefix = 0;

    final Matcher tagMatcher = TAG_PATTERN.matcher(subject);
    String tag = null;
    // whether tag stripping logic should be active
    boolean tagPresent = false;
    // whether the last action stripped a tag
    boolean tagStripped = false;
    if (tagMatcher.find(0)) {
        tagPresent = true;
        if (tagMatcher.start() == 0) {
            // found at beginning of subject, considering it an actual tag
            tag = tagMatcher.group();

            // now need to find response marker after that tag
            lastPrefix = tagMatcher.end();
            tagStripped = true;
        }
    }

    final Matcher matcher = RESPONSE_PATTERN.matcher(subject);

    // while:
    // - lastPrefix is within the bounds
    // - response marker found at lastPrefix position
    // (to make sure we don't catch response markers that are part of
    // the actual subject)

    while (lastPrefix < subject.length() - 1 && matcher.find(lastPrefix) && matcher.start() == lastPrefix
            && (!tagPresent || tag == null || subject.regionMatches(matcher.end(), tag, 0, tag.length()))) {
        lastPrefix = matcher.end();

        if (tagPresent) {
            tagStripped = false;
            if (tag == null) {
                // attempt to find tag
                if (tagMatcher.start() == lastPrefix) {
                    tag = tagMatcher.group();
                    lastPrefix += tag.length();
                    tagStripped = true;
                }
            } else if (lastPrefix < subject.length() - 1 && subject.startsWith(tag, lastPrefix)) {
                // Re: [foo] Re: [foo] blah blah blah
                //               ^     ^
                //               ^     ^
                //               ^    new position
                //               ^
                //              initial position
                lastPrefix += tag.length();
                tagStripped = true;
            }
        }
    }
    // Null pointer check is to make the static analysis component of Eclipse happy.
    if (tagStripped && (tag != null)) {
        // restore the last tag
        lastPrefix -= tag.length();
    }
    if (lastPrefix > -1 && lastPrefix < subject.length() - 1) {
        return subject.substring(lastPrefix).trim();
    } else {
        return subject.trim();
    }
}

From source file:com.android.mms.service.HttpUtils.java

/**
 * Resolve the macro in HTTP param value text
 * For example, "something##LINE1##something" is resolved to "something9139531419something"
 *
 * @param value The HTTP param value possibly containing macros
 * @return The HTTP param with macro resolved to real value
 */// w ww.  j  ava2  s  .  c o m
private static String resolveMacro(Context context, String value, MmsConfig.Overridden mmsConfig) {
    if (TextUtils.isEmpty(value)) {
        return value;
    }
    final Matcher matcher = MACRO_P.matcher(value);
    int nextStart = 0;
    StringBuilder replaced = null;
    while (matcher.find()) {
        if (replaced == null) {
            replaced = new StringBuilder();
        }
        final int matchedStart = matcher.start();
        if (matchedStart > nextStart) {
            replaced.append(value.substring(nextStart, matchedStart));
        }
        final String macro = matcher.group(1);
        final String macroValue = mmsConfig.getHttpParamMacro(context, macro);
        if (macroValue != null) {
            replaced.append(macroValue);
        } else {
            Log.w(TAG, "HttpUtils: invalid macro " + macro);
        }
        nextStart = matcher.end();
    }
    if (replaced != null && nextStart < value.length()) {
        replaced.append(value.substring(nextStart));
    }
    return replaced == null ? value : replaced.toString();
}

From source file:gov.nih.nci.ncicb.cadsr.loader.test.MainTestCase.java

static List getTableNames() throws Exception {
    if (tableNames != null) {
        return tableNames;
    } else {//from  w  w w  .  ja  va  2  s .c  o m
        tableNames = new ArrayList();
    }

    String ddl = getDDL();

    if (ddl != null) {

        BufferedReader reader = new BufferedReader(new StringReader(ddl));
        String line = null;

        while ((line = reader.readLine()) != null) {
            Matcher m = createTablePattern.matcher(line);
            if (m.find()) {
                int start = m.end();
                int end = line.length() - 1;
                String tableName = line.substring(start, end).trim();

                tableNames.add(tableName);
            }
        }
    }

    /*Map classMetaData = ((EntityManagerFactoryImpl)emf).getSessionFactory().getAllClassMetadata();
    Iterator tableClassesIter = classMetaData.keySet().iterator();
    while (tableClassesIter.hasNext()) {
       String className = (String)tableClassesIter.next();
       String tableName = className.substring(className.lastIndexOf(".")+1);
       tableNames.add(tableName);
    }*/
    return tableNames;
}

From source file:gov.nih.nci.ncicb.cadsr.bulkloader.util.MainTestCase.java

static List getTableNames() throws Exception {
    if (tableNames != null) {
        return tableNames;
    } else {/*w  w  w  . jav a 2  s.  c  o  m*/
        tableNames = new ArrayList();
    }

    String ddl = getDDL();

    if (ddl != null) {

        BufferedReader reader = new BufferedReader(new StringReader(ddl));
        String line = null;

        while ((line = reader.readLine()) != null) {
            Matcher m = createTablePattern.matcher(line);
            if (m.find()) {
                int start = m.end();
                int end = line.length();
                String tableName = line.substring(start, end).trim();

                tableNames.add(tableName);
            }
        }
    }

    /*Map classMetaData = ((EntityManagerFactoryImpl)emf).getSessionFactory().getAllClassMetadata();
    Iterator tableClassesIter = classMetaData.keySet().iterator();
    while (tableClassesIter.hasNext()) {
       String className = (String)tableClassesIter.next();
       String tableName = className.substring(className.lastIndexOf(".")+1);
       tableNames.add(tableName);
    }*/
    return tableNames;
}

From source file:com.tdclighthouse.commons.simpleform.validation.validator.item.PatternMatchValidator.java

private boolean performValidation(FormItem item, String param) {
    boolean result = true;
    item.setErrorMessage(null);/*w w  w.ja v  a2  s  .  c o m*/
    String value = item.getValue();
    if (!StringUtils.isBlank(value)) {
        value = value.trim();
        Pattern pattern = Pattern.compile(param);
        Matcher matcher = pattern.matcher(value);
        if (matcher.find()) {
            result = ((matcher.start() == 0) && (matcher.end() == value.length()));
        } else {
            result = false;
            item.setErrorMessage(getMessage());
        }
    }
    return result;
}

From source file:net.duckling.falcon.api.mq.impl.SimpleStringMatch.java

public String routingMatch(Map<String, String> regMap, String value) {
    if (StringUtils.isEmpty(value)) {
        return "";
    }//from   w  ww  .  ja  va2  s  .c  om
    if (regMap.keySet().contains(value)) {
        return value;
    }
    String matchedReg = null;
    for (Map.Entry<String, String> entry : regMap.entrySet()) {
        Pattern m = Pattern.compile(entry.getValue());
        Matcher matcher = m.matcher(value);
        while (matcher.find()) {
            if (matcher.start() == 0 && (matcher.end() == value.length())) {
                matchedReg = entry.getKey();
                return matchedReg;
            }
        }
    }
    return null;
}

From source file:com.zimbra.common.util.TemplateCompiler.java

private static void convertLines(PrintWriter out, String pkg, String lines, Map<String, String> attrs,
        boolean authoritative) {
    out.print("AjxTemplate.register(\"");
    out.print(pkg);/*from  ww  w  .  j  a  va2  s  . c om*/
    out.println("\", ");
    out.println("function(name, params, data, buffer) {");
    out.println("\tvar _hasBuffer = Boolean(buffer);");
    out.println("\tdata = (typeof data == \"string\" ? { id: data } : data) || {};");
    out.println("\tbuffer = buffer || [];");
    out.println("\tvar _i = buffer.length;");
    out.println();

    Matcher matcher = RE_REPLACE.matcher(lines);
    if (matcher.find()) {
        int offset = 0;
        do {
            int index = matcher.start();
            if (offset < index) {
                printStringLines(out, lines.substring(offset, index));
            }
            String param = matcher.group(1);
            String inline = matcher.group(2);
            if (param != null) {
                printDataLine(out, param);
            } else if (inline != null) {
                printBufferLine(out, inline);
            } else {
                printLine(out, "\t", matcher.group(3).replaceAll("\n", "\n\t"), "\n");
            }
            offset = matcher.end();
        } while (matcher.find());
        if (offset < lines.length()) {
            printStringLines(out, lines.substring(offset));
        }
    } else {
        printStringLines(out, lines);
    }
    out.println();

    out.println("\treturn _hasBuffer ? buffer.length : buffer.join(\"\");");
    out.println("},");
    if (attrs != null && attrs.size() > 0) {
        out.println("{");
        Iterator<String> iter = attrs.keySet().iterator();
        while (iter.hasNext()) {
            String aname = iter.next();
            String avalue = attrs.get(aname);
            out.print("\t\"");
            printEscaped(out, aname);
            out.print("\": \"");
            printEscaped(out, avalue);
            out.print("\"");
            if (iter.hasNext()) {
                out.print(",");
            }
            out.println();
        }
        out.print("}");
    } else {
        out.print("null");
    }
    out.print(", ");
    out.print(authoritative);
    out.println(");");
}

From source file:com.intuit.karate.Script.java

public static Pair<String, String> parseVariableAndPath(String text) {
    Matcher matcher = VAR_AND_PATH_PATTERN.matcher(text);
    matcher.find();//from w w w.  jav  a2  s  .co m
    String name = text.substring(0, matcher.end());
    String path;
    if (matcher.end() == text.length()) {
        path = "";
    } else {
        path = text.substring(matcher.end());
    }
    if (Script.isXmlPath(path) || Script.isXmlPathFunction(path)) {
        // xml, don't prefix for json
    } else {
        path = "$" + path;
    }
    return Pair.of(name, path);
}

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  ww . j a v a  2  s  . c om*/
    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  w  w w  .  j  ava 2s .  co  m
    return sb.toString();
}