Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: all ASCII characters but a certain character (PHP, FILTER_VALIDATE_REGEXP)

Tags:

regex

php

filter

Found the following nice regex to match all printable ASCII characters:

[ -~]

My code looks like this:

    $string = "My ASCII string is (not) very funny.";

    filter_var($string, FILTER_VALIDATE_REGEXP,
    array("options"=>array("regexp"=>"/^[ -~]*$/")));

Thats almost what I need however I want to exclude the colon. I have tried [ -~\:] and [ -~^:] which does not work. What is the correct regex and how to exclude single characters correctly?

like image 679
Blackbam Avatar asked Jan 23 '26 09:01

Blackbam


1 Answers

Looks like that you're looking for a regex like this:

(?=[ -~])[^:]

You can exclude also the semicolon by adding it to the "exclude list" :

(?=[ -~])[^:;]
like image 83
nicael Avatar answered Jan 25 '26 22:01

nicael