Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression in Qt

I am using Qt5.6.

I need to process the incoming data in a serial port, the data will be of the format "AD=+172345AD=+272345" and so on. I append the incoming data to a QString and using Regex to extract the decimals.

If I write a regular expression :

int tmp = StrData.indexOf(QRegularExpression("AD=\+[0-9]{6}"))

it doesn't match, i.e tmp is always -1. But I tested the regex here, and I found it to be valid. What could be the issue?

like image 626
Sivaram Avatar asked Jul 19 '26 13:07

Sivaram


1 Answers

As per the docs, you can use the regex inside QString.indexOf to get the index position of the first match of the regular expression re in the string.

The only problem with the regex is that in Qt, the strings are C style, i.e. they can contain escape sequences. Thus, the backslashes escaping regex special characters must be doubled.

Use

QRegularExpression("AD=\\+[0-9]{6}")

or

QRegularExpression("AD=[+][0-9]{6}")

since inside [...], the + is treated as a literal character.

like image 116
Wiktor Stribiżew Avatar answered Jul 21 '26 03:07

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!