Regex for Any English ASCII Character Including Special Characters

dav picture dav · Jul 23, 2014 · Viewed 8.8k times · Source

I want to write a regex in php to match only any english characters, spaces, numbers and all special chars.

From this question Regex any ascii character

I tried this

preg_match("/[\x00-\x7F]+/", $str);

but it throws a warning

No ending delimiter '/' found 

so, how to write this regex in php.

the alternative would be smth like [a-z\d\s] and also one by one consider all special chars, but is not there a way to do simpler ?

Thanks

Answer

zx81 picture zx81 · Jul 23, 2014

There are a number of intricate solutions, but I would suggest this beautifully simple regex:

^[ -~]+$

It allows all printable characters in the ASCII table.

In PHP:

$regex = '%^[ -~]+$%';
if (preg_match($regex, $yourstring, $m)) {
    $thematch = $m[0];
    } 
else { // no match...
     }

Explanation

  • The ^ anchor asserts that we are at the beginning of the string
  • The character class [ -~] matches all characters between the space and the tilde (all the printable characters in the ASCII table)
  • The + quantifier means "match one or more of those"
  • The $ anchor asserts that we are at the end of the string