Preg Match Empty String

前端 未结 5 1946
不知归路
不知归路 2020-12-30 20:19

How to preg_match for an null (empty) string??

I need something like:

/([0-9]|[NULL STRING])/
5条回答
  •  北海茫月
    2020-12-30 20:30

    I had to do this, in java, for the last 4 for a CC number. The field is optional so it could either be empty, or 4 digits. My solution is:

    ^\d{4}$|^$
    

    The is passing this a null in the typical java fashion results in a null pointer exception:

    String myString = null;
    last4Pattern.matcher(myString).matches(); //Null in this case.
    

    However, I feel that it is more of a Java implementation problem.

提交回复
热议问题