Just add |null :
[0-9]{5}|[0-9]{10}|null
As you probably know | is the "or" operator, and the null character string matches the word null. Thus, it can be considered as <your previous pattern> or null .
If you want the pattern to match the null string, the answer is that this is not possible. That is, you cannot do, for example, Matcher.matches() return true for a null input string. If this is what you need, you can get away with using the above expression and not match with str , but with ""+str , which will result in "null" if str actually null .
source share