문제

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