I have a QString with following content:
qDebug() << "BEFORE: " << data;
data = data.replace("\\\\x", "\\x", Qt::CaseSensitivity::CaseInsensitive);
qDebug() << "AFTER: " << data;
data = data.replace("\\x", "\x", Qt::CaseSensitivity::CaseInsensitive);
\x used with no following hex digits!
First let's look at what this piece of code does:
data.replace("\\\\x", "\\x", ....
First string becomes
\\x in compiled code, and is used as regular expression. In reqular expression, backslash is special, and needs to be escaped with another backslash to mean actual single backslash character, and your regexp does just this. 4 backslashes in C+n string literal regexp means matching single literal backslash in target text. So your reqular expression matches literal 2-character string
Then you replace it. Replacement isn't a reqular expression, so backslash doesn't need double escaping here, so you end up using literal 2-char replacement string
\x, which is same as what you matched, so even if there is a match, nothing changes.
However, this is not your problem, your problem is how
qDebug() prints strings. It prints them escaped. That
\" at start of output means just plain double quote, 1 char, in the actual string because double quote is escaped. And those
\\ also are single backslash char, because literal backslash is also escaped (because it is the escape char and has special meaning for the next char).
So it seems you don't need to do any search replace at all, just remove it.
Try printing the QString in one of these ways to get is shown literally:
std::cout << data << std::endl; qDebug() << data.toLatin1().constData();