03-26-2014 11:31 PM
I am trying to extract some text from a long string using QRegEx. Here is my regex:
QString url = "https://192.168.1.10/files/30/encrypt/key=s6h779bf
qDebug() << rx.cap(0);
qDebug() << rx.cap(1);
However, this is not matching, anything.
If i change the regex to "(192|210).*key=?*" then it match but i cant get the string after key=
Solved! Go to Solution.
03-27-2014 05:09 AM - edited 03-27-2014 05:11 AM
You need to use a regex capture if you want 'key' parameter
QRegExp rx("^.*(192|210).*key=(.*)?"); qDebug() << rx.cap(1); // here's the first octet of your IP qDebug() << rx.cap(2); // key parameter (optional)
Your regex could do with alot of improvement imho. You might want to consuder the QUrl class which does what you want already:
"); qDebug() << url.queryItemValue("key");
Tip: rubular is a really handy web-based regex tester that makes debugging this stuff much easier
03-29-2014 08:42 AM
Thank you, i got it work after adding the "()" and remove the ^. I can understand my first attempt did not work because i did not specify which part should be capture but i still dont get why the match fail everytime i put the ^ in at the beginning.