Вопрос

I am currently working on a Qt C++ application on Mac. Throughout the application I am using string pattern matching pretty frequently. When using the QRegExp class, I keep having trouble with something that I just don't understand!!!

My QRegExp looks like this :

QRegExp regEx("M|F\\dS\\d\\d.C\\d\\d", Qt::CaseInsensitive);

It is supposed to match patterns like "M1S02.C12" or "F4S14.C01" in filenames. This works great as soon as the input filenames, which should be matched don't contain parts of this pattern.

For example : my input file is named "testItem_abcd_M1S03.C02_grade3"

regEx.exactMatch("testItem_abcd_M1S03.C02_grade3");

returns false, whereas

regEx.indexIn("testItem_abcd_M1S03.C02_grade3");

returns 7, which is the position of "m" in "item".

Can someone please tell me, what I would need to do to make it match right?

Thanks in advance, guitarflow

Это было полезно?

Решение

The problem is that the | restricts to searching either M or F\dS\d\d.C\d\d. So the sinle m of item will match.

Try "[MF]\\dS\\d\\d\\.C\\d\\d" instead.
(Note: Also replaced . with \\., as a single dot matches any character)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top