Search code examples
phpregexspecial-characters

Regular Expression accepting alphanumeric and /.~?=+&


I need a validation expression that accepts alphanumeric characters and these special characters /.~?=+&

I'm doing this in php so it either gives me errors or not working at all

I tried this thing. But it don't accept even alphanumerics:

preg_match('/^[\w ]+([\.\~\?\=\+\&][\w ]*)?$/',$var)

Solution

  • You don't have to escape characters within []. The only ones that have to be escaped are \,/,[ and [. To match alphanumeric and your set of special characters try following:

    (\w|[\/.~?=+&])*