Example usage for java.util.regex Matcher replaceAll

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

Introduction

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

Prototype

public String replaceAll(Function<MatchResult, String> replacer) 

Source Link

Document

Replaces every subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence.

Usage

From source file:com.agiletec.plugins.jpnewsletter.aps.system.services.newsletter.NewsletterManager.java

protected String parseText(String defaultText, Map<String, String> params) {
    String body = defaultText;/*from ww  w .  j a va  2s.  c  o m*/
    for (Entry<String, String> pairs : params.entrySet()) {
        String regExp = "\\{" + pairs.getKey() + "\\}";
        Pattern pattern = Pattern.compile(regExp);
        Matcher codeMatcher = pattern.matcher("");
        codeMatcher.reset(body);
        if (codeMatcher.find()) {
            body = codeMatcher.replaceAll((String) pairs.getValue());
        }
    }
    return body;
}

From source file:com.cisco.dvbu.ps.deploytool.services.RegressionManagerUtils.java

/**
 * get the from clause for a view and the procedure path without the parameters if it is a procedure.
 * /*  w  w  w  . j  ava2 s .  c  om*/
 * Examples:
 *   incoming query string                                  outgoing result
 *   -----------------------                              ----------------
 *   SELECT COUNT(*) FROM VIEW1                           --> VIEW1
 *   SELECT COUNT(*) FROM CAT1.SCH1.VIEW1                  --> CAT1.SCH1.VIEW1
 *   SELECT COUNT(*) FROM CAT1.SCH1.VIEW1 WHERE X=1            --> CAT1.SCH1.VIEW1
 *   SELECT COUNT(*) FROM SCH1.LookupProcedure ( 1 )          --> SCH1.LookupProcedure
 *   SELECT COUNT(*) FROM SCH1.LookupProcedure ( 1 )  WHERE x=1   --> SCH1.LookupProcedure
 *   { CALL SCH1.LookupProcedure ( 1 ) }                  --> SCH1.LookupProcedure
 *   
 * @param query
 * @return tableURL
 */
public static String getTableUrl(String query) {
    String tableUrl = null;
    int regexSize = 102400;
    Pattern p = null;
    Matcher m = null;

    // Parse the FROM clause
    if (query.toUpperCase().indexOf("FROM") > 0) {
        String fromClause = query.substring(query.toUpperCase().indexOf("FROM") + 4).trim();

        // Create a pattern to match a space within double quotes
        p = Pattern.compile(" " + "(?=[^\"]{0," + regexSize + "}\"(?:[^\"\r\n]{0," + regexSize + "}\"[^\"]{0,"
                + regexSize + "}\"){0," + regexSize + "}[^\"\r\n]{0," + regexSize + "}$)");
        // Create a matcher with an input string
        m = p.matcher(fromClause);
        // Encode all spaces within double quotes "" with _0020
        fromClause = m.replaceAll("_0020");

        // Create a pattern to match a period within double quotes
        p = Pattern.compile("\\." + "(?=[^\"]{0," + regexSize + "}\"(?:[^\"\r\n]{0," + regexSize + "}\"[^\"]{0,"
                + regexSize + "}\"){0," + regexSize + "}[^\"\r\n]{0," + regexSize + "}$)");
        // Create a matcher with an input string
        m = p.matcher(fromClause);
        // Encode all periods within double quotes "" with _002e
        fromClause = m.replaceAll("_002e");

        //Loop through the fromClause and replace all occurrences of white space before and after a period separator
        boolean found = true;
        while (found) {
            if (fromClause.contains(" .") || fromClause.contains(". ")) {

                // Create a pattern to match " ."
                p = Pattern.compile(" \\.");
                // Create a matcher with an input string
                m = p.matcher(fromClause);
                // Replace all white space before a period
                fromClause = m.replaceAll("\\.");

                // Create a pattern to match ". "
                p = Pattern.compile("\\. ");
                // Create a matcher with an input string
                m = p.matcher(fromClause);
                // Replace all white space after a period
                fromClause = m.replaceAll("\\.");
                found = true;
            } else {
                found = false;
            }
        }
        // Extract the from clause
        int pos = fromClause.indexOf(" ");
        if (pos >= 0) {
            tableUrl = fromClause.substring(0, pos).trim();
        } else {
            tableUrl = fromClause.trim();
        }

        // Extract the procedure name from the open parenthesis
        int parenPos = tableUrl.indexOf("(");
        if (parenPos >= 0) {
            tableUrl = tableUrl.substring(0, parenPos).trim();
        }

        // Decode all encoded spaces _0020 with a space " "
        tableUrl = tableUrl.replaceAll("_0020", " ");
        // Decode all encoded periods _002e with a period "."
        tableUrl = tableUrl.replaceAll("_002e", ".");
    }

    // Extract the procedure CALL syntax
    if (query.toUpperCase().indexOf("CALL") > 0) {
        String proc = query.substring(query.toUpperCase().indexOf("CALL") + 4).trim();
        int pos = proc.indexOf("(");
        if (pos >= 0) {
            tableUrl = proc.substring(0, pos).trim();
        }
    }

    return tableUrl;
}

From source file:io.personium.core.rs.odata.AbstractODataResource.java

/**
 * ???TimeMillis??????.// w w w .ja  v  a 2  s .c o  m
 * @param timeStr TimeMillis??(ex."/Data(...)/", "SYSUTCDATETIME()")
 * @return TimeMillis?
 */
private long getTimeMillis(String timeStr) {
    long timeMillis = 0;
    if (timeStr.equals(Common.SYSUTCDATETIME)) {
        timeMillis = currentTimeMillis;
    } else {
        try {
            Pattern pattern = Pattern.compile("^/Date\\((.+)\\)/$");
            Matcher match = pattern.matcher(timeStr);
            if (match.matches()) {
                String date = match.replaceAll("$1");
                timeMillis = Long.parseLong(date);
            }
        } catch (NumberFormatException e) {
            throw PersoniumCoreException.OData.JSON_PARSE_ERROR.reason(e);
        }
    }
    return timeMillis;
}

From source file:com.android.launcher3.Utilities.java

/**
 * Trims the string, removing all whitespace at the beginning and end of the string.
 * Non-breaking whitespaces are also removed.
 *//*w  ww .  j  ava  2 s . c o  m*/
public static String trim(CharSequence s) {
    if (s == null) {
        return null;
    }

    // Just strip any sequence of whitespace or java space characters from the beginning and end
    Matcher m = sTrimPattern.matcher(s);
    return m.replaceAll("$1");
}

From source file:fr.natoine.html.HTMLPage.java

private void finalizeBody() {
    if (body != null && body.length() > 0) {
        Pattern p = Pattern.compile("(<body>)|(<BODY>)");
        Matcher m = p.matcher("");
        m.reset(body);/*from ww  w  .j a  va2 s .  com*/
        body = m.replaceAll("<div id='" + wrapperDiv + "'>");
        p = Pattern.compile("(</body>)|(</BODY>)");
        m = p.matcher("");
        m.reset(body);
        body = m.replaceAll("</div>");
    } else
        body = "";
}

From source file:fr.natoine.html.HTMLPage.java

public String deleteCommentsNewLine(String _wip_css, String _new_englobing_div) {
    if (_wip_css == null || _wip_css.length() == 0)
        return "";
    // Create a pattern to match comments
    //Pattern p = Pattern.compile("(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)|(?://.*)", Pattern.MULTILINE);
    Pattern p = Pattern.compile("(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)", Pattern.MULTILINE);
    Matcher m = p.matcher("");
    m.reset(_wip_css);/*ww  w.  j ava2s.  c  o m*/
    String result = m.replaceAll("");
    // Create a pattern to match all new lines and all tabs
    p = Pattern.compile("(\n)|(\t)");
    m = p.matcher("");
    m.reset(result);
    result = m.replaceAll("");
    // Create a pattern to match all multiple " "
    p = Pattern.compile(" (?= )|(?<= ) ");
    m = p.matcher("");
    m.reset(result);
    result = m.replaceAll(" ");
    //creates a pattern to match all }
    p = Pattern.compile("}");
    m = p.matcher("");
    m.reset(result);
    result = m.replaceAll("} #" + _new_englobing_div + " ");
    int cpt_last_spaces_index = result.length();
    while (cpt_last_spaces_index > 0 && result.charAt(cpt_last_spaces_index - 1) == ' ') {
        cpt_last_spaces_index--;
    }
    result = result.substring(0, cpt_last_spaces_index);
    if (result.endsWith("#" + _new_englobing_div))
        result = result.substring(0, result.lastIndexOf("#" + _new_englobing_div));
    result = ("#" + _new_englobing_div + " ").concat(result);
    return result;
}

From source file:com.rapidminer.tools.Tools.java

/**
 * Replaces all possible line feed character combinations by &quot;\n&quot;. This might be
 * important for GUI purposes like tool tip texts which do not support carriage return
 * combinations./*  w ww . j a va 2s. c  o m*/
 */
public static String transformAllLineSeparators(String text) {
    Pattern crlf = Pattern.compile("(\r\n|\r|\n|\n\r)");
    Matcher m = crlf.matcher(text);
    if (m.find()) {
        text = m.replaceAll("\n");
    }
    return text;
}

From source file:com.rapidminer.tools.Tools.java

/**
 * Removes all possible line feed character combinations. This might be important for GUI
 * purposes like tool tip texts which do not support carriage return combinations.
 *///  w ww . j a v  a2  s  . c o m
public static String removeAllLineSeparators(String text) {
    Pattern crlf = Pattern.compile("(\r\n|\r|\n|\n\r)");
    Matcher m = crlf.matcher(text);
    if (m.find()) {
        text = m.replaceAll(" ");
    }
    return text;
}

From source file:org.codehaus.groovy.grails.web.pages.GspTagParser.java

private String extractClassLevelCode(String text) {
    Matcher m = CLASS_LEVEL_PATTERN.matcher(text);
    StringBuilder sb = new StringBuilder();
    while (m.find()) {
        sb.append(m.group(1)).append('\n');
    }/*  w  w w  .  j a  v  a 2s .co  m*/
    classLevelCode = sb.toString();
    return m.replaceAll("");
}

From source file:org.apache.hadoop.fs.azure.NativeAzureFileSystem.java

/**
 * Azure Storage doesn't allow the blob names to end in a period,
 * so encode this here to work around that limitation.
 *//*ww  w  .  ja  v a  2  s  .com*/
private static String encodeTrailingPeriod(String toEncode) {
    Matcher matcher = TRAILING_PERIOD_PATTERN.matcher(toEncode);
    return matcher.replaceAll(TRAILING_PERIOD_PLACEHOLDER);
}