PDA

View Full Version : QRegExp documentation



cic
28th October 2013, 14:21
Hi, it must be an easy to solve problem i hope :)

what is the regular expression for the pattern:

G|xxxx - a capital letter | arbitrary word

I used the patterns below but they failed:



rule.pattern = QRegExp("\\b([A-Z])\\|([a-z])\\s");

or

rule.pattern = QRegExp("\\b([A-Z])|([a-z])\\s");


the thing is i have no idea how to express vertical bar in this case.
anyone can help?

ps: i find the documentation from QRegExp is somehow not clearly explain all the cases.
is there any better tutorials for that?

stampede
28th October 2013, 15:21
Vertical bar in your code is ok, but [a-z] is not "arbitraty word", for that you can use \w+ which means "any word character one or more times":

#include <QRegExp>
#include <QStringList>
#include <QDebug>

int main(){
QRegExp rx("^([A-Z])\\|(\\w+)$");
QStringList str = QStringList() << "G|word" << "_K|another" << "z|maybethis" << "P|this_one_for_sure" << "Z|!@#";
foreach (QString s, str){
if (rx.indexIn(s) > -1){
qDebug() << rx.capturedTexts();
}
}
return 0;
}