[SOLVED] QString and regular expressions
-
When I call QString::contains on the following string "\?\USB#VID_0FE2&PID", I always return false. If I just use "VID_0FE2" as the string constant, I can get the function to return true. I do however need the prior form of the string. Any way to resolve this? Is it that I need to use a regular expression?
Thanks in advance.
-
Hi,
If you want to use the regex variant of contains, you'll need to do this:
@myString.contains(QRegExp(“\?\USB#VID_0FE2&PID”))@
Note: you can use the regex demo in Qt's source to ensure the one you use will work as expected, it also provide the "escaped" version you need to put in your code.
Hope it helps
-
If you are using Qt5, QRegularExpression should be a better choice
"QRegularExpression":http://dangelog.wordpress.com/2012/04/07/qregularexpression/
-
[quote author="SGaist" date="1369947498"]Hi,
If you want to use the regex variant of contains, you'll need to do this:
@myString.contains(QRegExp(“\?\USB#VID_0FE2&PID”))@
Note: you can use the regex demo in Qt's source to ensure the one you use will work as expected, it also provide the "escaped" version you need to put in your code.
Hope it helps[/quote]
Thanks anyway, it doesn't work. I still return a false and I'm using Qt 4.8.4.
-
You are not stating clearly your string issu, can you show
- what string you are getting
- what regex you are using
- what result you are expecting
-
Im trying to see if a USB descriptor contains the VID that I am looking for. I search all connected devices and what's returned are similar to the literal that I posted. As I stated before, it works if I leave out the escape characters and just use “VID_0FE2”. I am trying to get a true condition for the contains method. Perhaps the regex string used is wonky and don't have a grasp on forming the regex string.
-
So you need something like this ?
@"VID_[a-zA-Z0-9]{4}"@
-
My code:
@
QString qs = QString::fromUtf16(DetailData->DevicePath);
qs = qs.toUpper();if(qs.contains(QRegExp("VID_[a-zA-Z0-9]{4}"))) { qDebug() << qs << " contains the USB vendor ID"; if (deviceInfo) { SetupDiDestroyDeviceInfoList(deviceInfo); break; } }
@
Function returns true on the first pass which is wrong:
@
"\?\usb#vid_0e0f&pid_0003#6&b77da92&0&1#{a5dcbf10-6530-11d2-901f-00c04fb951ed}"
@I need it true for VID_0FE2
-
I would also like to help but to re-iterate:
[quote author="SGaist" date="1370031066"]You are not stating clearly your string issu, can you show-
what string you are getting
-
what regex you are using
-
what result you are expecting
[/quote] -
So what is the output of qs after line 2.
If it is the line in your second code block then your RegExp will not work to match the start since the backslash is a special command in RegExps, you need something like (escape the slashes):
@
QRegExp(“\\?\usb#vid_[a-zA-Z0-9]{4}&pid”)
@(note: using modified version using comment from SGaist)
Note: tThat is for if you keep the string lower case (not do line 2 on the string). If you still want to do toUpper(), replace the lower case letters with uppercase in the pattern.
Use the Regular Expression Example to evaluate such 'simple' patters, the "Escaped Pattern" is then the version that you put in the code.
-
-
[quote author="Badger" date="1370262432"]I would also like to help but to re-iterate:
[quote author="SGaist" date="1370031066"]You are not stating clearly your string issu, can you show-
what string you are getting
-
what regex you are using
-
what result you are expecting
[/quote] -
So what is the output of qs after line 2.
If it is the line in your second code block then your RegExp will not work to match the start since the backslash is a special command in RegExps, you need something like (escape the slashes):
@
QRegExp(“\\?\usb#vid_[a-zA-Z0-9]{4}&pid”)
@(note: using modified version using comment from SGaist)
Note: tThat is for if you keep the string lower case (not do line 2 on the string). If you still want to do toUpper(), replace the lower case letters with uppercase in the pattern.
Use the Regular Expression Example to evaluate such 'simple' patters, the "Escaped Pattern" is then the version that you put in the code. [/quote]
The output after line 2 is just the upper case version of what I posted previously:
@"\?\USB#VID_0E0F&PID_0003#6&B77DA92&0&1#{A5DCBF10-6530-11D2-901F-00C04FB951ED}"@
I suspect it was the RegExp string that wasn't working correctly. The modified version based you posted upon the suggestion from SGaist did not work either. What is shown to work is the call using:
@
if(qs.contains(QRegExp("\\?\USB#VID_0FE2&PID")))
@Thanks to both of you for the help.
-