PDA

View Full Version : How to match "(" in a regexp?



Batox
2nd February 2011, 21:45
I want to extract the "6" from "Foo (6)" into a variable.

In for example TCL/TK I have used "\(([0-9].)\)" in such situations. But this doesn't work, the compiler complains about an unknown backslash escape sequence (obviously the "\(" and "\)").

So how can I quote the round braces?

Lykurg
2nd February 2011, 21:55
You have to double escape the \ . It looks very nasty but it must be done:
QString s = "(123) (333s) (2)";
QRegExp rx("\\(([0-9]+)\\)");
int pos = 0;
while ((pos = rx.indexIn(s, pos)) != -1) {
qWarning() << rx.cap(1);
pos += rx.matchedLength();
}

Batox
2nd February 2011, 23:44
Of course - I have to quote the "\" so that after the compiler has finished there's still a "\" there for the regexp. Thanks a lot :)