I'm struggling with REGEX and require it for a program.
The input require only alphanumerical keys and also (allow only comma,:,space,/,- in special chars)
I have tried = (^[a-zA-Z0-9,:\S/-]*$) As far as i understand and please correct me if I'm wrong. a-zA-Z0-9 - The alphanumerical keys. ,: - Comma and colon \S - Space / - I'm not sure how to represent a forward slash thus i escaped it - - Dash also not sure if it is needed to escape it.
Would be appreciated if this can be corrected and also a explanation of each part.
Thanks in advance.
You can replace a-zA-Z0-9
with just \\w
which is short for [a-zA-Z_0-9]
. Furthermore, \\S
is any character, but not a whitespace, you should use a \\s
instead. You don't need to escape /
, and even -
if it's the first one or the last one, because if it's placed between two characters it could be interpreted as range and you'll have to escape it. So, you can make your regex like ^([\w,:\s/-]*)$
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments