-1

I am trying to convert a camel cased QString into lowercased words separated by spaces. I currently have:

QString camelCase = "thisIsACamelCaseWord"
QString unCamelCase = camelCase.replace(QRegularExpression("([A-Z])", " $1")).toLower();

Which seems to work here,

"this Is A Camel Case Word"

but it is returning with:

"this $1s $1 $1amel $1ase $1ord"

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Daniel Lord
  • 754
  • 5
  • 18
  • 1
    Since Qt is using PCRE you should also use the back reference syntax used [by pcre](https://pcre.org/original/doc/html/pcrepattern.html) - \0, \1 and so on. – chehrlic Jun 03 '22 at 05:00
  • @chehrlic, that made it work. – Daniel Lord Jun 03 '22 at 06:29
  • @chehrlic any chance you write a proper answer? I hate it when answer is written in comments and in a way that doesn't open up for non-experts of the topic. This would be a great point to learn about pcre and back end reference syntax but answer is only serving the OP and not others. So I'd be happy to up-vote a correct answer which teaches me something new ;) – talamaki Jun 03 '22 at 07:55

1 Answers1

2

Since QRegularExpression uses PRCE the back reference syntax is '\0', '\1' and so on as explained in the documentation.

chehrlic
  • 913
  • 1
  • 5
  • 5
  • Just to clarify, use a double backslash instead of $: `QRegularExpression("([A-Z])", " \\1")` – Morne Aug 02 '22 at 03:24