I tried to search for an answer to this for a while but could not find it. There were many posts related to matching text which is not preceeded by certain text but none seems to work for this case where + is matched but it is allowed only when preceeded by a single + (eg. ++)
I am trying to remove punctuation marks from text but let two consecutive ++ signs to stay but single + signs to disappear
$text="Hello World! C+ C++ C#";
print_r(preg_replace('/(?!\+\+)[[:punct:]]/', ' ', $text));
Hello World C C+ C
$text="Hello World! C+ C++ C#";
print_r(preg_replace('/(?!\+)[[:punct:]]/', ' ', $text));
Hello World C+ C++ C
Hello World C C++ C
Hello World C C++ C#
You have a couple of choices here, one being:
(?<!\+)[+#](?!\+)
# with lookarounds making sure no + is after/behind
PHP
:
<?php
$regex = '~(?<!\+)[+#](?!\+)~';
$string = 'Hello World! C+ C++ C#';
$string = preg_replace($regex, '', $string);
echo $string;
?>
(*SKIP)(*FAIL)
mechanism (which is a bit faster in this example):
\+{2}(*SKIP)(*FAIL)|[+#]
# let two consecutive ++ always fail
See a demo for this one on regex101.com as well.
Last but not least: If you want to add characters/expressions that should be avoided as well, you can put them in a non-capturing group and let this one fail:
(?:\#|\+{2})(*SKIP)(*FAIL)|
[[:punct:]]
Yet another demo on the wonderful regex101.com site.