Example usage for java.util.regex Matcher appendReplacement

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

Introduction

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

Prototype

public Matcher appendReplacement(StringBuilder sb, String replacement) 

Source Link

Document

Implements a non-terminal append-and-replace step.

Usage

From source file:Main.java

public static String replaceHtmlTag(String str, String beforeTag, String tagAttrib, String startTag,
        String endTag) {/* www.  j  av  a 2s .co  m*/
    String regxpForTag = "<\\s*" + beforeTag + "\\s+([^>]*)\\s*>";
    String regxpForTagAttrib = tagAttrib + "=\"([^\"]+)\"";
    Pattern patternForTag = Pattern.compile(regxpForTag);
    Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib);
    Matcher matcherForTag = patternForTag.matcher(str);
    StringBuffer sb = new StringBuffer();
    boolean result = matcherForTag.find();
    while (result) {
        StringBuffer sbreplace = new StringBuffer();
        Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag.group(1));
        if (matcherForAttrib.find()) {
            matcherForAttrib.appendReplacement(sbreplace, startTag + matcherForAttrib.group(1) + endTag);
        }
        matcherForTag.appendReplacement(sb, sbreplace.toString());
        result = matcherForTag.find();
    }
    matcherForTag.appendTail(sb);
    return sb.toString();
}

From source file:Main.java

public static String checkMobileNum(String phoneNum) {
    if (TextUtils.isEmpty(phoneNum))
        return "";

    Pattern p1 = Pattern.compile("^((\\+{0,1}86){0,1})1[0-9]{10}");
    Matcher m1 = p1.matcher(phoneNum);
    if (m1.matches()) {
        Pattern p2 = Pattern.compile("^((\\+{0,1}86){0,1})");
        Matcher m2 = p2.matcher(phoneNum);
        StringBuffer sb = new StringBuffer();
        while (m2.find()) {
            m2.appendReplacement(sb, "");
        }/*from w  w  w.j  a  v a  2 s  . co  m*/
        m2.appendTail(sb);
        return sb.toString();
    } else {
        return phoneNum;
    }
}

From source file:Main.java

public static String decodeXML(String inXML) {
    Pattern p1 = Pattern.compile("&lt;");
    Matcher m1 = p1.matcher(inXML); // get a matcher object
    StringBuffer sb1 = new StringBuffer();
    while (m1.find()) {
        m1.appendReplacement(sb1, "<");
    }/*  www.jav  a  2 s .  c  o m*/
    inXML = m1.appendTail(sb1).toString();

    Pattern p2 = Pattern.compile("&gt;");
    Matcher m2 = p2.matcher(inXML); // get a matcher object
    StringBuffer sb2 = new StringBuffer();
    while (m2.find()) {
        m2.appendReplacement(sb2, ">");
    }
    String outXML = m2.appendTail(sb2).toString();

    return outXML;
}

From source file:Main.java

public static String encodeXML(String inXML) {
    Pattern p1 = Pattern.compile("<");
    Matcher m1 = p1.matcher(inXML); // get a matcher object
    StringBuffer sb1 = new StringBuffer();
    while (m1.find()) {
        m1.appendReplacement(sb1, "&lt;");
    }// www. ja va  2 s .  c om
    inXML = m1.appendTail(sb1).toString();

    Pattern p2 = Pattern.compile(">");
    Matcher m2 = p2.matcher(inXML); // get a matcher object
    StringBuffer sb2 = new StringBuffer();
    while (m2.find()) {
        m2.appendReplacement(sb2, "&gt;");
    }
    String outXML = m2.appendTail(sb2).toString();

    return outXML;
}

From source file:Main.java

public static String getAttributeNameFromColName(String tableName) {
    Pattern p = Pattern.compile("_+(\\w?)");
    Matcher m = p.matcher(tableName);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        if (m.start() != 0)
            m.appendReplacement(sb, m.group(1).toUpperCase());
        else//from  w ww. ja  va  2  s.c  o m
            m.appendReplacement(sb, m.group(1).toLowerCase());
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:com.beyondjservlet.gateway.servlet.support.ProxySupport.java

private static void appendReplacement(final Matcher m, final StringBuffer sb, final String name,
        final String value) {
    m.appendReplacement(sb, ';' + name + '=' + (value == null ? m.group(2) : value));
}

From source file:Main.java

private static String lowerCaseAttributes(String formatted) {
    Matcher m = ID_PATTERN.matcher(formatted);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        String text = m.group();// w ww .  j  ava2  s  .  c  o  m
        m.appendReplacement(sb, Matcher.quoteReplacement(text.toLowerCase()));
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:Main.java

public static String filterHtml(String str) {
    Pattern pattern = Pattern.compile(regxpForHtml);
    Matcher matcher = pattern.matcher(str);
    StringBuffer sb = new StringBuffer();
    boolean result1 = matcher.find();
    while (result1) {
        matcher.appendReplacement(sb, "");
        result1 = matcher.find();/*from   ww w. j  a  va  2 s.com*/
    }
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:Main.java

public static String fiterHtmlTag(String str, String tag) {
    String regxp = "<\\s*" + tag + "\\s+([^>]*)\\s*>";
    Pattern pattern = Pattern.compile(regxp);
    Matcher matcher = pattern.matcher(str);
    StringBuffer sb = new StringBuffer();
    boolean result1 = matcher.find();
    while (result1) {
        matcher.appendReplacement(sb, "");
        result1 = matcher.find();/*from  w  ww .  j  a v a2s .  co  m*/
    }
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:Main.java

public static String fixCharCode(String wholeFile) {
    long millis = System.currentTimeMillis();
    Matcher mat = CHAR_CODE_PATTERN.matcher(wholeFile);
    StringBuffer sb = new StringBuffer();
    while (mat.find()) {
        // System.gc();
        mat.appendReplacement(sb, ((char) Integer.parseInt(mat.group(1)) + ""));
    }//from  ww  w  .ja v a2s . com
    mat.appendTail(sb);
    Log.d("fix char code time: ", "" + (System.currentTimeMillis() - millis));
    return sb.toString();
}