Java RegEx pattern is invalid when trying to exclude commas -
i'm building function validate usernames, , in case want accept alphabetic characters only. i'm matching provided user input against regex:
[1-9!@#$%&*()_+=|<>?{}\\[\\]~-,] this method makes use of regex:
public static string purgeinvalidlogin(string failedlogin, string pattern) { pattern special = pattern.compile (pattern); string purgedlogin = failedlogin.replaceall(special.pattern(), ""); // remove special characters before moving on purgedlogin = stringutils.deletewhitespace(purgedlogin); return purgedlogin; } however when trying run message:
illegal character range near index 25 [!@#$%&*()_+=|<>?{}[]~-,] ^
which happened once added comma. i've tried expression [!@#$%&*()_+=|<>?{}[]~-\,] (escaping comma) no avail. i'm wondering how can use regex exclude commas making use of method above.
thanks in advance.
escape hyphen before it. interpreted defining range of characters, add character (the comma) after it.
[1-9!@#$%&*()_+=|<>?{}\\[\\]~\\-,]
Comments
Post a Comment