I have a string that I want to match with php regex.
$string = "~word1 ~word2 ~word3 word4";
I want to match all words that are not start with ~ sign. In php I have tried this, but it doesn't work.
preg_match("/(?!~)(?<words>[a-zA-Z0-9\.\_])/i", $string, $matches);
var_dump($matches);
For this purpose you may use this regex with a negative lookbehind:
RegEx Demo
RegEx Details:
(?<!~): Negative lookbehind to fail the match if~is at previous position\b: Word boundary\w: Match a word character[\w.-]*: Match 0 or more of word character or.or-