0

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$

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

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

4
  • And what the expected output for a null b? I would expect ab. Commented Oct 21, 2013 at 9:07
  • 1
    use CASE_INSENSITIVE and take out ^ $ Commented Oct 21, 2013 at 9:08
  • “a null b” the output has to be “a null b” Commented Oct 21, 2013 at 9:21
  • Try with "(?i)null" as regular expression. Please see my below answer for reference Commented Oct 21, 2013 at 9:32

5 Answers 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);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

try with (?i) in regular expression as in my below answer
regex is very close to what I was expecting... Thanks
so matching stuff like {null] was actually ok? This would actually match also (null
{null] is matching. but there is a catch as micantox mentioned... It is also matching (null or null) - which should not be the case
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",""));

3 Comments

What about (null), [null] and {null}?
Thank,.. but this will not match (null)
I didn't see this requirement in the original post. Sorry.
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

Comments

0

Try this:

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

2 Comments

Eww. Ever heard of case-insensitive matching? And this will also match [null), though I don't believe it should.
A kitten is killed every time you use this regex
-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();
    }

2 Comments

At first look this won't work. What about blabla {null} bla?
What about (null), [null] and {null}?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.