Trouble parsing using simple QRegExp
I wish to extract some parts of a formatted string, so I opted to use QRegExp. I tested the expression so I know it works alright, but can't seem to get it to work using Qt:confused: .
A snippet of test code is below - it is supposed to dump the sub-expressions into a text file.
Any help is appreciated.
JS
Code:
Temp.setFileName("temp.txt");
// I want to extract "PATIENT0001/", "STUDY0001/", and "SERIES0001 "
QRegExp rx
("[a-zA-Z0-9]*[\/\\ ]{1,2}");
QString str
= "PATIENT0001/STUDY0001/SERIES0001 DIRECTORY_1";
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1)
{
Output << rx.cap(1)<<"\n";
pos += rx.matchedLength();
}
Re: Trouble parsing using simple QRegExp
Try:
Code:
QRegExp rx
( "([a-zA-Z0-9]*[/\\ ]){1,2}" );
// or
QRegExp rx
( "(?:([a-zA-Z0-9]*)[/\\ ]){1,2}" );
Re: Trouble parsing using simple QRegExp
It did not work. Could there be something else wrong with this code? I used examples from Qt Assistant and had no trouble. I suspect the regular expression is poorly formatted but cannot be sure.:(
Another suggestion?
Code:
QString Str
("PATIENT0001/STUDY0001/SERIES0001 DIRECTORY_1");
QRegExp rexp
("(?:([a-zA-Z0-9]*)[/\\ ]){1,2}");
int pos, Count;
Output <<"The following is the output from the regular expression:\n";
while((pos = rexp.indexIn(Str, pos)) != -1)
{
Output<<rexp.cap(1);
pos += rexp.matchedLength();
}
Re: Trouble parsing using simple QRegExp
Then try:
Code:
QRegExp rx
( "([a-zA-Z0-9]*[/\\ ])" );
Re: Trouble parsing using simple QRegExp
Quote:
Originally Posted by jacek
Then try:
Code:
QRegExp rx
( "([a-zA-Z0-9]*[/\\ ])" );
It works, but will trigger that lonely "1" at the end or a standalone / \ or 0x20 too.
How about:
Code:
QRegExp rx
("([a-zA-Z][a-zA-Z0-9]*[/\\ ])");
or:
Code:
QRegExp rc
("([A-Za-z]+[0-9]+(/|\\)?)");
BTW. The regexp in the opening post is fine too (but it catches that lonely '1' at the end too). Just checked it. Something else has to be wrong there. Maybe it just lacked brackets?