Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
Posts: 20
Registered: ‎03-23-2014
My Device: z10
My Carrier: fido
Accepted Solution

How to extract matched string from regex

Hello

 

I am trying to extract some text from a long string using QRegEx. Here is my regex:

 

QRegExp rx("^.*(192|210).*key=?*");

QString url = "https://192.168.1.10/files/30/encrypt/key=s6h779bf"

rx.setPatternSyntax(QRegExp::Wildcard);

rx.indexIn(str, 0);

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=

 

 

Developer
Posts: 291
Registered: ‎10-15-2012
My Device: bb10 developer
My Carrier: Orange

Re: How to extract matched string from regex

[ Edited ]

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:

 

QUrl url("https://192.168.1.10/files/30/encrypt/key=s6h779bf");
 
qDebug() << url.queryItemValue("key");

 

Tip: rubular is a really handy web-based regex tester that makes debugging this stuff much easier

 

http://rubular.com/

 

New Developer
Posts: 20
Registered: ‎03-23-2014
My Device: z10
My Carrier: fido

Re: How to extract matched string from regex

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.