Java Regex String Replace All replaceAllStrictly(String src, String search, String replacement, boolean entirelyMatch, boolean caseSensitive)

Here you can find the source of replaceAllStrictly(String src, String search, String replacement, boolean entirelyMatch, boolean caseSensitive)

Description

ignore regex

License

Open Source License

Declaration

public static String replaceAllStrictly(String src, String search, String replacement, boolean entirelyMatch,
        boolean caseSensitive) 

Method Source Code

//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**/*ww w  . j  a v  a2 s. c o  m*/
     * ignore regex
     * 
     */
    public static String replaceAllStrictly(String src, String search, String replacement, boolean entirelyMatch,
            boolean caseSensitive) {
        // case 1:
        if (search == null) {
            if (src == null) {
                return replacement; // regex == null && src == null
            } else {
                return src; // regex == null && src != null
            }
        } else {
            // case 2:
            if (src == null) {
                return null; // regex != null && src == null
            } else {
                // case 3:
                if (replacement == null || entirelyMatch) {
                    if ((caseSensitive && src.equals(search)) || (!caseSensitive && src.equalsIgnoreCase(search))) {
                        // regex != null && src != null && replacement != null, and match the whole src
                        return replacement;
                    } else {
                        return src; // can't match the whole src
                    }

                } else {
                    int flag = caseSensitive ? Pattern.LITERAL : Pattern.LITERAL | Pattern.CASE_INSENSITIVE;
                    return Pattern.compile(search, flag).matcher(src)
                            .replaceAll(Matcher.quoteReplacement(replacement));
                }
            }
        }
    }

    /**
     * to discuss the case: src == null || regex == null || replacement == null
     * 
     */
    public static String replaceAll(String src, String regex, String replacement) {

        // case 1:
        if (regex == null) {
            if (src == null) {
                return replacement; // regex == null && src == null
            } else {
                return src; // regex == null && src != null
            }
        } else {
            // case 2:
            if (src == null) {
                return null; // regex != null && src == null
            } else {
                // case 3:
                if (replacement == null) {
                    if (src.matches(regex)) {
                        // regex != null && src != null && replacement != null, and match the whole src
                        return replacement;
                    } else {
                        return src; // can't match the whole src
                    }

                } else {
                    // regex != null && src != null && replacement != null
                    return src.replaceAll(regex, replacement);

                }
            }
        }
    }
}

Related

  1. replaceAll(StringBuilder sb, String regex, String replacement)
  2. replaceAllBackreference(String text, String regex, String replacement)
  3. replaceAllIgnoreCase(String source, String oldstring, String newstring)
  4. replaceAllLiteral(@Nonnull String value, @Nonnull Pattern pattern, @Nonnull String replace)
  5. replaceAllPerLine(Pattern pattern, String value, String replacement)
  6. replaceAllTotal(String what, Pattern p, String replacement)
  7. replaceAllTotal(String what, String expr, String replacement)