0
4

5 回答 5

2

You can truy with this pattern: (?i)[(\\[{]?null[)\\]}]?. Please, see below example:

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

import org.apache.commons.lang3.StringUtils;

public class EnumProgram {

    public static void main(String... args) {
        test("a NULL b", "a  b");
        test("a null b", "a  b");
        test("a Null b", "a  b");
        test("a NuLl b", "a  b");
        test("a (null) b", "a  b");
        test("a [null] b", "a  b");
        test("a {null} b", "a  b");
    }

    private static void test(String value, String expected) {
        String newValue = Util.removeNullString(value);
        System.out.println(value + " : " + newValue.equals(expected));
    }
}

class Util {
    private static Pattern pattern = Pattern.compile("(?i)[(\\[{]?null[)\\]}]?");

    public static String removeNullString(String value) {
        if (StringUtils.isEmpty(value)) {
            return StringUtils.EMPTY;
        }

        Matcher matcher = pattern.matcher(value);
        return matcher.replaceAll(StringUtils.EMPTY);
    }
}
于 2013-10-21T09:25:39.283 回答
0

What you describe is called a "case-insensitive" regular expression. Not being a Java expert, I found this SO answer which has the following line, edited to fit your needs:

System.out.print(sample.replaceAll("(?i)\\b(?:null)\\b",""));
于 2013-10-21T09:05:35.060 回答
0

Try this:

str.replaceAll("(?i)\\(null\\)|\\[null\\]|\\{null\\}|null", "");
于 2013-10-21T09:11:02.950 回答
0

Use (?i) to validate case insensitive in regular expression. See the below code for example. The below code will output "a b"

String str = "a NULl b";
String regx = "(?i)null";
String str2 = str.replaceAll(regx, "");
System.out.println(str2);

output : a b
于 2013-10-21T09:30:35.853 回答
-1

Try this,

private static String removeNull(String input)
    {
        String[] splittedValue = input.split(" ");
        StringBuilder stringBuilder = new StringBuilder();
        for (String value : splittedValue)
        {
            if (!value.equalsIgnoreCase("null"))
            {
                stringBuilder.append(value);
                stringBuilder.append(" ");
            }
        }
        return stringBuilder.toString();
    }
于 2013-10-21T09:12:01.117 回答