Question

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

Was it helpful?

Solution

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top