PDA

View Full Version : Trouble parsing using simple QRegExp



johnny_sparx
23rd February 2006, 23:19
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



QFile Temp;
QTextStream Output(&Temp);

Temp.setFileName("temp.txt");
Temp.open(QIODevice::WriteOnly | QIODevice::Text);

// 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();
}

jacek
23rd February 2006, 23:37
Try:
QRegExp rx( "([a-zA-Z0-9]*[/\\ ]){1,2}" );
// or
QRegExp rx( "(?:([a-zA-Z0-9]*)[/\\ ]){1,2}" );

johnny_sparx
23rd February 2006, 23:55
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?


QString Str("PATIENT0001/STUDY0001/SERIES0001 DIRECTORY_1");
QRegExp rexp("(?:([a-zA-Z0-9]*)[/\\ ]){1,2}");
QStringList Parsed_List;

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();
}

jacek
24th February 2006, 00:02
Then try:
QRegExp rx( "([a-zA-Z0-9]*[/\\ ])" );

wysota
24th February 2006, 00:42
Then try:
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:


QRegExp rx("([a-zA-Z][a-zA-Z0-9]*[/\\ ])");

or:


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?