So I want to validate a string based on whether or not it contains only grammatical characters as well as numbers and letters.(basically A-Z, a-z, 0-9, plus periods(.), commas(,) colons(:), semicolons(;), hyphens(-), single quotes('), double quotes(") and parentheses(). I am getting a PHP error that says "Compilation failed: range out of order in character clas". What regex code should I be using?
This is the one I'm currently using: ^[a-zA-Z0-9_:;-()'\" ]*^
You need to escape -
which would then become this ^[a-zA-Z0-9_:;\-()'\" ]*
. -
has a special meaning inside character set so it needs to be escaped. ^
in the end is also not necessary. The regex can also simplified using \w
like this
^[\w:;()'"\s-]*
\w
matches letters, digits, and underscores.
The problem is that you have a dash character in the regex, which the parser is interpreting as a range instead of as a literal dash. You can fix that by:
^[a-zA-Z0-9_:;\-()'\" ]*^
)^[-a-zA-Z0-9_:;()'\" ]*^
)^[a-zA-Z0-9_:;()'\" -]*^
)