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:Main.java

public static String toHref(String title) {
    StringBuffer sb = new StringBuffer(title);
    Pattern pat = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    Matcher mat = pat.matcher(title);
    int index = 0;
    int index1 = 0;
    while (mat.find()) {
        String url = mat.group();
        //System.out.println(url);
        if (url.indexOf("http://") != 0)
            url = "http://" + url;
        Object obj[] = { "'" + url + "'" };
        String a = MessageFormat.format(A1, obj);
        int l = a.length();
        index += index1;/* ww w  . ja  va 2  s  .  c  o  m*/
        sb.insert(mat.start() + index, a);
        index += l;
        sb.insert((mat.end()) + index, A2);
        index1 = A2.length();
    }
    return sb.toString();
}

From source file:com.ery.ertc.estorm.util.ToolUtil.java

public static String ReplaceRegex(Matcher m, String substitution) {
    try {//from ww w  . j av a 2  s .  co m
        Matcher vm = valPartsRegex.matcher(substitution);
        String val = substitution;
        String regpar = substitution;
        int gl = m.groupCount();
        while (vm.find()) {
            regpar = regpar.substring(vm.end());
            int g = Integer.parseInt(vm.group(1));
            if (g > gl) {
                val = val.replaceAll("\\$\\d", "");
                break;
            }
            String gv = m.group(Integer.parseInt(vm.group(1)));
            if (gv != null)
                val = val.replaceAll("\\$" + g, gv);
            else
                val = val.replaceAll("\\$" + g, "");
            vm = valPartsRegex.matcher(regpar);
        }
        return val;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.googlecode.promnetpp.main.Main.java

private static void preprocessSourceCode() {
    StringBuilder sourceCodeAsBuilder = new StringBuilder(sourceCode);
    String commentRegex = "/[*].*?[*]/";
    Pattern commentPattern = Pattern.compile(commentRegex, Pattern.DOTALL);
    Matcher commentMatcher = commentPattern.matcher(sourceCodeAsBuilder);
    while (commentMatcher.find()) {
        String comment = commentMatcher.group().replace("/*", "").replace("*/", "").trim();
        boolean isAnnotatedComment = comment.startsWith("@");
        if (!isAnnotatedComment) {
            //Remove the comment and reset the matcher
            sourceCodeAsBuilder.delete(commentMatcher.start(), commentMatcher.end());
            commentMatcher = commentPattern.matcher(sourceCodeAsBuilder);
        }/*w ww .  j  av  a2 s.  com*/
    }
    sourceCode = sourceCodeAsBuilder.toString();
}

From source file:PropertiesHelper.java

/**
 * Adds new properties to an existing set of properties while
 * substituting variables. This function will allow value
 * substitutions based on other property values. Value substitutions
 * may not be nested. A value substitution will be ${property.key},
 * where the dollar-brace and close-brace are being stripped before
 * looking up the value to replace it with. Note that the ${..}
 * combination must be escaped from the shell.
 *
 * @param a is the initial set of known properties (besides System ones)
 * @param b is the set of properties to add to a
 * @return the combined set of properties from a and b.
 *//*from   www.ja v  a  2s .com*/
protected static Properties addProperties(Properties a, Properties b) {
    // initial
    Properties result = new Properties(a);
    Properties sys = System.getProperties();
    Pattern pattern = Pattern.compile("\\$\\{[-a-zA-Z0-9._]+\\}");

    for (Enumeration e = b.propertyNames(); e.hasMoreElements();) {
        String key = (String) e.nextElement();
        String value = b.getProperty(key);

        // unparse value ${prop.key} inside braces
        Matcher matcher = pattern.matcher(value);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            // extract name of properties from braces
            String newKey = value.substring(matcher.start() + 2, matcher.end() - 1);

            // try to find a matching value in result properties
            String newVal = result.getProperty(newKey);

            // if still not found, try system properties
            if (newVal == null) {
                newVal = sys.getProperty(newKey);
            }

            // replace braced string with the actual value or empty string
            matcher.appendReplacement(sb, newVal == null ? "" : newVal);
        }
        matcher.appendTail(sb);
        result.setProperty(key, sb.toString());
    }
    // final
    return result;
}

From source file:StringUtils.java

public static final List<String> explode(String string, Pattern separator) {
    int lastIndex = 0, len = string.length();

    Matcher matcher = separator.matcher(string);
    List<String> ret = new LinkedList<String>();

    while (matcher.find()) {
        String s = string.substring(lastIndex, matcher.start());
        if (s.length() > 0)
            ret.add(s);/*  ww  w  .j a v a 2  s . c o  m*/
        lastIndex = matcher.end();
    }
    String s = string.substring(lastIndex, len);
    if (s.length() > 0)
        ret.add(s);

    return ret;
}

From source file:com.centurylink.mdw.util.ExpressionUtil.java

/**
 * Substitutes dynamic values for expressions in the input string.
 * @param input raw input string/*  w ww .  j  a  v a 2s  .  co m*/
 * @param variables variable instances to use in substitutions
 * @return string with values substituted
 */
public static String substitute(String input, List<VariableInstance> variables) throws MdwException {
    StringBuffer substituted = new StringBuffer(input.length());
    try {
        Matcher matcher = tokenPattern.matcher(input);
        int index = 0;
        while (matcher.find()) {
            String match = matcher.group();
            substituted.append(input.substring(index, matcher.start()));
            Object value = getVariableValue(match.substring(2, match.length() - 1), variables);
            if (value != null)
                substituted.append(value);
            index = matcher.end();
        }
        substituted.append(input.substring(index));
        return substituted.toString();
    } catch (Exception ex) {
        throw new MdwException("Error substituting expression value(s) in input: '" + input + "'", ex);
    }
}

From source file:com.sun.socialsite.util.UtilitiesModel.java

/**
 * Transforms the given String into a subset of HTML displayable on a web
 * page. The subset includes &lt;b&gt;, &lt;i&gt;, &lt;p&gt;, &lt;br&gt;,
 * &lt;pre&gt; and &lt;a href&gt; (and their corresponding end tags).
 *
 * @param s   the String to transform/*  w  w w .j  a  va 2s .  c o m*/
 * @return    the transformed String
 */
public static String transformToHTMLSubset(String s) {

    if (s == null) {
        return null;
    }

    s = replace(s, OPENING_B_TAG_PATTERN, "<b>");
    s = replace(s, CLOSING_B_TAG_PATTERN, "</b>");
    s = replace(s, OPENING_I_TAG_PATTERN, "<i>");
    s = replace(s, CLOSING_I_TAG_PATTERN, "</i>");
    s = replace(s, OPENING_BLOCKQUOTE_TAG_PATTERN, "<blockquote>");
    s = replace(s, CLOSING_BLOCKQUOTE_TAG_PATTERN, "</blockquote>");
    s = replace(s, BR_TAG_PATTERN, "<br />");
    s = replace(s, OPENING_P_TAG_PATTERN, "<p>");
    s = replace(s, CLOSING_P_TAG_PATTERN, "</p>");
    s = replace(s, OPENING_PRE_TAG_PATTERN, "<pre>");
    s = replace(s, CLOSING_PRE_TAG_PATTERN, "</pre>");
    s = replace(s, OPENING_UL_TAG_PATTERN, "<ul>");
    s = replace(s, CLOSING_UL_TAG_PATTERN, "</ul>");
    s = replace(s, OPENING_OL_TAG_PATTERN, "<ol>");
    s = replace(s, CLOSING_OL_TAG_PATTERN, "</ol>");
    s = replace(s, OPENING_LI_TAG_PATTERN, "<li>");
    s = replace(s, CLOSING_LI_TAG_PATTERN, "</li>");
    s = replace(s, QUOTE_PATTERN, "\"");

    // HTTP links
    s = replace(s, CLOSING_A_TAG_PATTERN, "</a>");
    Matcher m = OPENING_A_TAG_PATTERN.matcher(s);
    while (m.find()) {
        int start = m.start();
        int end = m.end();
        String link = s.substring(start, end);
        link = "<" + link.substring(4, link.length() - 4) + ">";
        s = s.substring(0, start) + link + s.substring(end, s.length());
        m = OPENING_A_TAG_PATTERN.matcher(s);
    }

    // escaped angle brackets
    s = s.replaceAll("&amp;lt;", "&lt;");
    s = s.replaceAll("&amp;gt;", "&gt;");
    s = s.replaceAll("&amp;#", "&#");

    return s;
}

From source file:edu.illinois.cs.cogcomp.wikifier.utils.freebase.QueryMQL.java

public static String decodeMQL(String s) {
    StringBuffer sb = new StringBuffer();
    int last = 0;
    Matcher m = quotedCharPattern.matcher(s);
    while (m.find()) {
        int start = m.start();
        int end = m.end();
        if (start > last) {
            sb.append(s.substring(last, start));
        }//ww w .j  av a  2  s . com
        last = end;
        sb.append((char) Integer.parseInt(s.substring(start + 1, end), 16));
    }

    if (last < s.length()) {
        sb.append(s.substring(last));
    }

    return sb.toString();
}

From source file:com.sun.socialsite.util.UtilitiesModel.java

/**
 * Code (stolen from Pebble) to add rel="nofollow" string to all links in HTML.
 *///from  w  ww  .  j a v  a2s.c  om
public static String addNofollow(String html) {
    if (html == null || html.length() == 0) {
        return html;
    }
    Matcher m = mLinkPattern.matcher(html);
    StringBuffer buf = new StringBuffer();
    while (m.find()) {
        int start = m.start();
        int end = m.end();
        String link = html.substring(start, end);
        buf.append(html.substring(0, start));
        if (link.indexOf("rel=\"nofollow\"") == -1) {
            buf.append(link.substring(0, link.length() - 1) + " rel=\"nofollow\">");
        } else {
            buf.append(link);
        }
        html = html.substring(end, html.length());
        m = mLinkPattern.matcher(html);
    }
    buf.append(html);
    return buf.toString();
}

From source file:org.yamj.core.service.mediaimport.FilenameScanner.java

private static String cutMatch(String rest, Matcher matcher) {
    return (rest.substring(0, matcher.start()) + rest.substring(matcher.end())).trim();
}