Question

I'm trying to replace \\u0061 and \u0061 to %u0061 with QRegExp,

So I did this,

QString a = "\\u0061";
qDebug() << a.replace(QRegExp("\\?\\u"), "%u");

Since the slash can appear either once or twice, so I used a ? to represen the first slash, but it ain't working, what's wrong about it?

EDIT

Thanks to Denomales, it should be \\\\u that represents \\u, and I'm using \\\\+u right now.

Was it helpful?

Solution

Description

Per the QT qregex documentation , see the section on Characters and Abbreviations for Sets of Characters:

Note: The C++ compiler transforms backslashes in strings. To include a \ in a regexp, enter it twice, i.e. \\. To match the backslash character itself, enter it four times, i.e. \\\\.

Care to give this a try:

[\\\\]{1,2}(u)

enter image description here

I've entered 4 backslashes so the various language layers can escape the backslash correctly. Then nested it inside square brackets and required it to appear 1 to 2 times. Essentially this should find the single and double backslashes before the letter u. You could then just replace with %u as in your example.

In my example the u character is captured and should be returned as group 1 to be used later in your replacement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top