Remove every occurrence of special characters in QString
How can I remove every occurrence of the special characters ^ and $ in a QString?
I tried:
QString str = "^TEST$^TEST$";
str = str.remove(QRegularExpression("[^$]."));
+3
leon22
source
to share
1 answer
You could not escape ^
. To avoid this, it is required \
, but also necessary, to be escaped because of the C strings. Also, you want one or more inputs to match +
. This regular expression should work: [\\^$]+
, watch online .
So it should be:
QString str = "^TEST$^TEST$";
str = str.remove(QRegularExpression("[\\^$]+"));
Another possibility, described in the comments below from Joe P :
QString str = "^TEST$^TEST$";
str = str.remove(QRegularExpression("[$^]+"));
because it ^
only has a special meaning at the beginning where you have to avoid it, to get it literally, look online .
+3
Andre Kampling
source
to share