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

public static List<String> extractMessageIds(final String text) {
    List<String> messageIds = new ArrayList<String>();
    Matcher matcher = MESSAGE_ID.matcher(text);

    int start = 0;
    while (matcher.find(start)) {
        String messageId = text.substring(matcher.start(), matcher.end());
        messageIds.add(messageId);//ww w . j  a  va  2  s.  c  o m
        start = matcher.end();
    }

    return messageIds;
}

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  ava 2s  .  co  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:net.krautchan.data.KCPosting.java

public static String sanitizeContent(String inContent) {
    String locContent = inContent;
    locContent = StringEscapeUtils.unescapeHtml4(locContent);
    locContent = locContent.replaceAll("<p>", "");
    locContent = locContent.replaceAll("</p>", " ");

    locContent = locContent.replaceAll("onclick=\"highlightPost\\(\\'\\d+\\'\\);\"", "");
    locContent = locContent.replaceAll(">>>(\\d+)</a>",
            " onclick='quoteClick(this); return false;' class=\"kclink\">&gt;&gt; $1</a>");

    locContent = locContent.replaceAll("<a href=\"/resolve(/.+?)\"\\s*>.+?</a>",
            "<a href=\"/resolve$1\" class=\"kclink\" onclick=\"Android.openKcLink('$1');return false;\">&gt;&gt; $1</a>");
    Matcher m = linkPat.matcher(locContent);
    StringBuffer buf = new StringBuffer(locContent.length() + 1000);
    int end = 0;//w  ww .j  ava 2 s  . co m
    while (m.find()) {
        int gc = m.groupCount();
        if (gc > 0) {
            buf.append(locContent.substring(end, m.start()));
            end = m.end();
            String host = m.group(1);
            String name = host;
            String styleClass = "extlink";
            String androidFunction = "openExternalLink";
            String url = m.group(1) + "/" + m.group(2);
            if ((host.contains("youtube")) || (host.contains("youtu.be"))) {
                styleClass = "ytlink";
                name = "YouTube";
                androidFunction = "openYouTubeVideo";
            } else if (host.contains("krautchan.net")) {
                styleClass = "kclink";
                name = ">>";
                host = "";
                androidFunction = "openKcLink";
            }
            buf.append("<a href=\"http://" + m.group(1) + "/" + m.group(2) + "\" class=\"" + styleClass
                    + "\" onclick=\"Android." + androidFunction + "('" + url + "');return false;\">" + name
                    + "</a>" + m.group(3));
        }
    }
    buf.append(locContent.substring(end, locContent.length()));
    return "<p><span>" + buf.toString().trim() + "</span></p>";
}

From source file:net.sf.zekr.engine.search.tanzil.RegexUtils.java

License:asdf

/**
 * Translate a symbolic regular expression into a legal one.
 * //from   ww w.  j  av a  2  s  .  c  om
 * @param str symbolic regex
 * @return legal regex
 */
public static final String regTrans(String str) {
    StringBuffer ret = new StringBuffer();
    Matcher matcher = REGTRANS_PATTERN.matcher(str);

    int lastEnd = 0;
    while (matcher.find()) {
        String group = matcher.group(1);

        String replacement;
        if (GROUPS.containsKey(group))
            replacement = (String) GROUPS.get(group);
        else if (CHARS.containsKey(group))
            replacement = ((Character) CHARS.get(group)).toString();
        else
            continue;
        ret.append(str.substring(lastEnd, matcher.start()));
        ret.append(replacement);
        lastEnd = matcher.end();
    }
    ret.append(str.substring(lastEnd));
    return ret.toString();
}

From source file:com.krawler.portal.tools.SourceFormatter.java

private static String _formatTaglibQuotes(String fileName, String content, String quoteType) {

    String quoteFix = StringPool.APOSTROPHE;

    if (quoteFix.equals(quoteType)) {
        quoteFix = StringPool.QUOTE;/* w w  w . ja  v a 2 s  .  c om*/
    }

    Pattern pattern = Pattern.compile(_getTaglibRegex(quoteType));

    Matcher matcher = pattern.matcher(content);

    while (matcher.find()) {
        int x = content.indexOf(quoteType + "<%=", matcher.start());
        int y = content.indexOf("%>" + quoteType, x);

        while ((x != -1) && (y != -1)) {
            String result = content.substring(x + 1, y + 2);

            if (result.indexOf(quoteType) != -1) {
                int lineCount = 1;

                char contentCharArray[] = content.toCharArray();

                for (int i = 0; i < x; i++) {
                    if (contentCharArray[i] == CharPool.NEW_LINE) {
                        lineCount++;
                    }
                }

                if (result.indexOf(quoteFix) == -1) {
                    StringBuilder sb = new StringBuilder();

                    sb.append(content.substring(0, x));
                    sb.append(quoteFix);
                    sb.append(result);
                    sb.append(quoteFix);
                    sb.append(content.substring(y + 3, content.length()));

                    content = sb.toString();
                } else {
                    logger.debug("taglib: " + fileName + " " + lineCount);
                }
            }

            x = content.indexOf(quoteType + "<%=", y);

            if (x > matcher.end()) {
                break;
            }

            y = content.indexOf("%>" + quoteType, x);
        }
    }

    return content;
}

From source file:com.gs.obevo.db.apps.reveng.AquaRevengMain.java

private static Pair<Integer, String> getStartIndex(String str, Pattern p) {
    Matcher m = p.matcher(str);
    while (m.find()) {
        String objectName = m.groupCount() > 0 ? m.group(1) : null; // by convention, the second group collected has the name
        return Tuples.pair(m.start(), objectName);
    }//from   ww w  .  j  a va  2 s  .  c o  m
    return Tuples.pair(Integer.MAX_VALUE, null);
}

From source file:com.sonyericsson.jenkins.plugins.bfa.tokens.Token.java

/**
 * Wrap some text/*w  w  w  .j  a v  a2s. c om*/
 * @param text some text to wrap
 * @param width the text will be wrapped to this many characters
 * @return the text lines
 */
/* package private */ static List<String> wrap(final String text, final int width) {
    final List<String> lines = new ArrayList<String>();
    final Splitter lineSplitter = Splitter.on(Pattern.compile("\\r?\\n"));
    //Split the text into lines
    for (final String line : lineSplitter.split(text)) {
        if (width > 0) {
            final Pattern firstNonwhitespacePattern = Pattern.compile("[^\\s]");
            final Matcher firstNonwhiteSpaceMatcher = firstNonwhitespacePattern.matcher(line);
            String indent = "";
            if (firstNonwhiteSpaceMatcher.find()) {
                indent = line.substring(0, firstNonwhiteSpaceMatcher.start());
            }
            //Wrap each line
            final String wrappedLines = WordUtils.wrap(line, width - indent.length());
            //Split the wrapped line into lines and add those lines to the result
            for (final String wrappedLine : lineSplitter.split(wrappedLines)) {
                lines.add(indent + wrappedLine.trim());
            }
        } else {
            lines.add(line);
        }
    }
    return lines;
}

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();

        if (endIdx - idx > 0) {
            statements.add(sql.substring(idx, endIdx));
        }/*from  w  w w  . j  av  a  2s  .c o m*/

        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:org.cleverbus.common.Strings.java

/**
 * Removes the HTML sequences from an input string.
 *
 * @param str the input string/* w w  w  .  jav a 2 s .  c om*/
 * @return the char sequence without HTML
 */
@Nullable
public static CharSequence removeHtml(@Nullable String str) {
    if (!isEmpty(str)) {
        StringBuilder sb = new StringBuilder();

        Matcher m = HTML_REMOVE_PATTERN.matcher(str);

        int last = 0;
        while (m.find()) {
            sb.append(str.substring(last, m.start()).replace('"', '\''));
            last = m.end();
        }
        sb.append(str.substring(last));

        return sb;
    }

    return str;
}

From source file:com.asakusafw.workflow.executor.TaskExecutors.java

/**
 * Resolves a path string using the current context.
 * @param context the current task execution context
 * @param path the target path expression
 * @return the resolved path//from   w  ww .ja  v a2s .c  om
 */
public static String resolvePath(TaskExecutionContext context, String path) {
    Matcher matcher = PATTERN_VARIABLE.matcher(path);
    int start = 0;
    StringBuilder buf = new StringBuilder();
    while (matcher.find(start)) {
        buf.append(path, start, matcher.start());
        switch (matcher.group(1)) {
        case VAR_USER:
            buf.append(getUserName(context));
            break;
        case VAR_BATCH_ID:
            buf.append(context.getBatchId());
            break;
        case VAR_FLOW_ID:
            buf.append(context.getFlowId());
            break;
        case VAR_EXECUTION_ID:
            buf.append(context.getExecutionId());
            break;
        default:
            throw new IllegalArgumentException(
                    MessageFormat.format("unknown variable \"{1}\": {0}", path, matcher.group()));
        }
        start = matcher.end();
    }
    buf.append(path, start, path.length());
    return buf.toString();
}