How can I change the following regular expression to make empty values (a.k.a String.Empty) also match:
^[0-9]{4}$
I’ve tried the following but have had no success:
^[0-9]{4}$|^$
(^[0-9]{4}$)*
I’m using a VB.Net Application and comparing with the namespace System.Text.RegularExpressions.
Note: The regular expression is a four-digit number representing the last four of an SSN; and it is optional. Therefore, I would like to resolve the match with one regular expression than have to do something like this:
If myString.Length = 0 Then
'This is Ok
Else
'Do the regular expression comparison here
End if
Try this pattern:
It’ll match the input having 0-4 digits.
If you only want 0 or 4 digits, use this:
Demo: http://regexhero.net/tester/?id=cd85169b-f5f8-457e-8f8e-759816213fb5