Java regex to remove NULL string

时间:2021-08-06 16:52:21

I want to replace the following string with Blank String ("") if it is equal to “NULL”,“null”, “Null”, “nuLl”, “(null)”, “[null]”, {null} etc. I am finding it difficult to cater all the scenarios with the below regex

我想用空字符串(“”)替换以下字符串,如果它等于“NULL”,“null”,“Null”,“nuLl”,“(null)”,“[null]”,{null}我发现很难用下面的正则表达式来满足所有场景

^NULL$

So if the input is “a null b” the output will be “a null b”

因此,如果输入为“null b”,则输出将为“null b”

But if the input is “null” output will be “”

但如果输入为“null”,则输出将为“”

5 个解决方案

#1


2  

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

您可以使用这种模式:(?i)[(\\ [{]?null [)\\]}]?请看下面的例子:

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);
    }
}

#2


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:

您描述的内容称为“不区分大小写”的正则表达式。不是Java专家,我发现这个SO答案有以下一行,编辑以满足您的需求:

System.out.print(sample.replaceAll("(?i)\\b(?:null)\\b",""));

#3


0  

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

使用(?i)验证正则表达式中的不区分大小写。例如,请参阅以下代码。以下代码将输出“a b”

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

output : a b

#4


0  

Try this:

str.replaceAll("(?i)\\(null\\)|\\[null\\]|\\{null\\}|null", "");

#5


-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();
    }

#1


2  

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

您可以使用这种模式:(?i)[(\\ [{]?null [)\\]}]?请看下面的例子:

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);
    }
}

#2


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:

您描述的内容称为“不区分大小写”的正则表达式。不是Java专家,我发现这个SO答案有以下一行,编辑以满足您的需求:

System.out.print(sample.replaceAll("(?i)\\b(?:null)\\b",""));

#3


0  

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

使用(?i)验证正则表达式中的不区分大小写。例如,请参阅以下代码。以下代码将输出“a b”

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

output : a b

#4


0  

Try this:

str.replaceAll("(?i)\\(null\\)|\\[null\\]|\\{null\\}|null", "");

#5


-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();
    }