3

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?

IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
Sivaram
  • 155
  • 1
  • 8

1 Answers1

4

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563