How to Prevent Exclamations Using Regular Expression

public static final String REGEX_ADDRESS_ZIP = "^[0-9\\ -.]+$"; 

The above regex for checking a zip code is like an exclamation (!), Although I did not allow it here. Not sure what the mistake is? I need to change the regex pattern

+7
java regex
source share
1 answer

A hyphen - a metacharacter inside character classes if it is not the first or last character. Change it to:

 ^[0-9\\ .-]+$ 

[0-9\\ -.] Means any character from 0 to 9 (all digits), a backslash \ and any character from a space (ASCII 32) to the period (ASCII 46) which translates to :

  !"#$%&'()*+,-. 
+10
source share

All Articles