When you put a bunch of characters in a character class, as in [hello] , this defines a token that matches a single character, which is either h, e, l, or o. In addition, | doesn't make sense inside a character class - it just matches a normal character.
The correct solution is not to use a character class - you should use regular brackets:
(\n|,|\.|".$exp.")
By the way, make sure you avoid any regular expression metacharacters found in $exp . In principle, the complete list here should be escaped using backslashes: http://regular-expressions.info/reference.html There may be an auxiliary function for this.
EDIT: since you are not using a character class, now we need to escape from \ . , which is now a metacharacteristic meaning of "match one out of nothing." Almost forgot.
source share