PDA

View Full Version : Define a QRegExp



aaditya190
25th November 2013, 10:36
Is there a way to define a QRegExp that would contain {}... e.g there is a regular expression defined as QRegExp rx("\\b[a-z]"); for characters a-z. Can we do it for only {} as well? If yes, can anyone help me with some code?

stampede
25th November 2013, 12:00
What have you tried so far ?

aaditya190
28th November 2013, 18:19
I have tried something like this... QRegExp rx("\\b{}");.. but this is now working. Can you suggest me the right way to do this as i want to include curly braces {} as a part of rx...

stampede
28th November 2013, 18:31
(...) include curly braces {} as a part of rx..
So there should be something between those brackets, right ? Your regexp captures only expressions starting with curly brackets (exps like "{}", "{}whatever",...).
Can you give some examples of what you want to capture with this regexp ?

aaditya190
29th November 2013, 06:11
Like i have tried some examples.. to include characters A-Z, i used QRegExp rx("\\b{A-Z}");.. this works.. What i want to know is what shall i do to include curly braces {} also as a part of the expression? Whatever we write in {} is included in the expression.. What to write to include {} as a part of the expression??

stampede
29th November 2013, 09:55
Sorry my previous post was wrong - curly brackets are special characters used when you want to define a number of occurences of an expression, so in order to match them, you have to use backslash:


QRegExp rx("\\{(\\w|\\s)+\\}");
qDebug() << rx.indexIn("{this one}") << rx.indexIn("not this one");

Sorry I totally forgot about this.

aaditya190
29th November 2013, 16:22
Thanks for the reply.. But this piece of code is also not working..
Instead i tried this .. QRegExp rx("\\{()+\\}");.. This worked.. When it encounters simultaneous {}, it works. What i want to do is to make individual curly braces i.e { and } work independently. Can you help me with this?

stampede
29th November 2013, 17:25
But this piece of code is also not working..
This regular expression works without problems - it all depends on what you want to capture.

What i want to do is to make individual curly braces i.e { and } work independently.
Use \\{ to match {, and \\} to match }. What else do you need ?

aaditya190
2nd December 2013, 06:29
It worked.. Thank You very much....