Example usage for java.util.regex Matcher appendTail

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

Introduction

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

Prototype

public StringBuilder appendTail(StringBuilder sb) 

Source Link

Document

Implements a terminal append-and-replace step.

Usage

From source file:Main.java

/**
 * Invokes append tail on matcher with the given string buffer, and returns
 * the string buffer as a string.// ww w .  ja va2 s  .  c om
 *
 * @param matcher the matcher.
 * @param sb      the string buffer.
 * @return a string.
 */
public static String appendTail(Matcher matcher, StringBuffer sb) {
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:Main.java

public static String html2Text(String html) {
    StringBuffer filterText = new StringBuffer();

    Pattern p = Pattern.compile("<([^>]*)>");
    Matcher m = p.matcher(html);
    while (m.find()) {
        m.appendReplacement(filterText, "");
    }//from  w w  w .jav a  2  s .co m
    m.appendTail(filterText);

    return unescapeHTML(filterText.toString());
}

From source file:Main.java

public static StringBuffer removePatternContents(String defStr, Pattern pat) {
    Matcher mat = pat.matcher(defStr);
    StringBuffer sb = new StringBuffer();
    while (mat.find()) {
        mat.appendReplacement(sb, " ");
    }//w ww . j  a va 2 s.  co  m
    mat.appendTail(sb);
    return sb;
}

From source file:Main.java

public static String getClassNameFromTableName(String tableName) {
    Pattern p = Pattern.compile("(_+|^)(\\w?)");
    Matcher m = p.matcher(tableName);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, m.group(2).toUpperCase());
    }/*from   w w  w  .j  a v a2 s . c  om*/
    m.appendTail(sb);

    return sb.toString();
}

From source file:Main.java

private static String urlEncode(String str, String charset) throws UnsupportedEncodingException {
    Pattern p = Pattern.compile("[\u4e00-\u9fa5]+");
    Matcher m = p.matcher(str);
    StringBuffer b = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(b, URLEncoder.encode(m.group(0), charset));
    }/*from  w  w  w.j a  v  a2 s.c o  m*/
    m.appendTail(b);
    return b.toString();
}

From source file:Main.java

/**
 * Remove xml comments from the xml string.
 * /* ww  w  . ja  va 2  s.  c o  m*/
 * @param xml
 * @return
 */
public static String stripXMLComment(CharSequence xml) {
    if (xml == null) {
        return null;
    }

    // option DOTALL: '.' matches newlines
    // use '.+?' in stead of '.*': non-greedy matching ("<!-- --> <important-stuff\> <!-- -->" is replaced with " <important-stuff\> ")
    Pattern p = Pattern.compile("<!--.+?-->", Pattern.DOTALL); //$NON-NLS-1$
    Matcher m = p.matcher(xml);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, ""); //$NON-NLS-1$
    }
    m.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, "");
        }/*w ww  .j  a v a2  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, "<");
    }/*from w w  w . j av a  2s  .  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;");
    }//w  w w. j  a  v a  2s .  c o m
    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

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