1

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.

daisy
  • 22,498
  • 29
  • 129
  • 265
  • I'm not exactly sure what brand of regex qt4 uses, but some popular brands of regex require four back slashes to represent one back slash, unless you can somehow indicate a raw string. – Justin O Barber May 26 '13 at 01:17
  • 1
    What happens if you use `QRegExp("\\{1,2}u")`? – lurker May 26 '13 at 01:31
  • @mbratch no luck, still `\u0061` – daisy May 26 '13 at 01:45
  • 1
    According to `QRegExp` documentation, the syntax rules used by can be changed with `setPatternSyntax()`. For example, if it were set to QRegExp::FixedString, then the pattern to be matched would be interpreted as a plain string, i.e., special characters (e.g., backslash) would not be escaped. Did you set the rules to anything in particular? – lurker May 26 '13 at 01:57
  • Then it would be good to know what they are by default, since it's clearly not matching a single backslash with two of them. – lurker May 26 '13 at 02:13

1 Answers1

2

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.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43
  • +1, but please don't use images of text when you can type in the text directly. If you did that to prevent the backslashes from being eaten, be aware that you can use code formatting inside blockquotes (as I demonstrated in my edit). – Alan Moore May 28 '13 at 02:01
  • I did that to show that the text was not edited or created by me. But thanks for the tip, I'll keep that in mind for the future. – Ro Yo Mi May 28 '13 at 04:42