Results 1 to 5 of 5

Thread: Trouble parsing using simple QRegExp

  1. #1
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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 .

    A snippet of test code is below - it is supposed to dump the sub-expressions into a text file.

    Any help is appreciated.

    JS

    Qt Code:
    1. QFile Temp;
    2. QTextStream Output(&Temp);
    3.  
    4. Temp.setFileName("temp.txt");
    5. Temp.open(QIODevice::WriteOnly | QIODevice::Text);
    6.  
    7. // I want to extract "PATIENT0001/", "STUDY0001/", and "SERIES0001 "
    8. QRegExp rx("[a-zA-Z0-9]*[\/\\ ]{1,2}");
    9. QString str = "PATIENT0001/STUDY0001/SERIES0001 DIRECTORY_1";
    10.  
    11. int pos = 0;
    12. while ((pos = rx.indexIn(str, pos)) != -1)
    13. {
    14. Output << rx.cap(1)<<"\n";
    15. pos += rx.matchedLength();
    16. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Trouble parsing using simple QRegExp

    Try:
    Qt Code:
    1. QRegExp rx( "([a-zA-Z0-9]*[/\\ ]){1,2}" );
    2. // or
    3. QRegExp rx( "(?:([a-zA-Z0-9]*)[/\\ ]){1,2}" );
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2006
    Posts
    47
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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?

    Qt Code:
    1. QString Str("PATIENT0001/STUDY0001/SERIES0001 DIRECTORY_1");
    2. QRegExp rexp("(?:([a-zA-Z0-9]*)[/\\ ]){1,2}");
    3. QStringList Parsed_List;
    4.  
    5. int pos, Count;
    6.  
    7. Output <<"The following is the output from the regular expression:\n";
    8. while((pos = rexp.indexIn(Str, pos)) != -1)
    9. {
    10. Output<<rexp.cap(1);
    11. pos += rexp.matchedLength();
    12. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Trouble parsing using simple QRegExp

    Then try:
    Qt Code:
    1. QRegExp rx( "([a-zA-Z0-9]*[/\\ ])" );
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Trouble parsing using simple QRegExp

    Quote Originally Posted by jacek
    Then try:
    Qt Code:
    1. QRegExp rx( "([a-zA-Z0-9]*[/\\ ])" );
    To copy to clipboard, switch view to plain text mode 
    It works, but will trigger that lonely "1" at the end or a standalone / \ or 0x20 too.

    How about:

    Qt Code:
    1. QRegExp rx("([a-zA-Z][a-zA-Z0-9]*[/\\ ])");
    To copy to clipboard, switch view to plain text mode 

    or:

    Qt Code:
    1. QRegExp rc("([A-Za-z]+[0-9]+(/|\\)?)");
    To copy to clipboard, switch view to plain text mode 

    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?
    Last edited by wysota; 24th February 2006 at 00:49.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.