regex - QRegexp Missing Digits -


we stumped on one:

qregexp kcc_stationing("(-)?(\\d+)\\.(\\d+)[^a-za-z]"); qstring str; if (kcc_stationing.indexin(description) > -1) {      str = kcc_stationing.cap(1) + kcc_stationing.cap(2) + "." + kcc_stationing.cap(3);             qdebug() << kcc_stationing.cap(1);             qdebug() << kcc_stationing.cap(2);             qdebug() << kcc_stationing.cap(3);             qdebug() << "description: " << description;             qdebug() << "returned stationing string: " << str; } 

running code on "1082.006":

enter image description here

note missing "6"

after blind guessing, removed [^a-za-z] , got correct answer. added reject number other characters directly attached without spaces.

for example: 10.05d should rejected.

can explain why piece causing lose last "6"?

the [^a-za-z] character class. character classes match 1 character. not match end of string, since there no character there.

to result, engine match numbers \\d+, including last one. need backtrack in order last character class satisfied.

i think want allow zero-width match (specifically when it's end of string). in case, easiest use:

(-)?(\\d+)\\.(\\d+)([^a-za-z]|$) 

or, if qt supports non-capturing groups:

(-)?(\\d+)\\.(\\d+)(?:[^a-za-z]|$) 

note recommend using [.] instead of \\., since feel improves readability.


Comments