Results 1 to 7 of 7

Thread: QRegExp Help

  1. #1
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question QRegExp Help

    I am trying to split a string using QRegExp but am having no luck. An example string of what I am looking for is:

    AB-123456 or CD-123456.78. As you can see, they all will begin with 2 letters, followed by a -, followed by 6 numbers. Then a "." with 1-4 numbers is optional. Currently, I have a QString that looks as follows: "AB-123456CD-123456.78EF-123456". I need to split this into its 3 parts: AB-123456, CD-123456.78, EF-123456. Here is my code. The QStringList returns a 1-item list with the whole string returned. Any ideas.

    Qt Code:
    1. QString text = "AB-123456CD-123456.78EF-123456";
    2. QRegExp rx( "^[A-Z]{2,2}-[0-9]{6,6}(.[0-9]{1,4})?$" );
    3. rx.setPatternSyntax( QRegExp::Wildcard );
    4. QStringList strList = text.split( rx, QString::SkipEmptyParts );
    To copy to clipboard, switch view to plain text mode 

    I thought by using the Wildcard syntax, then the "." would just be a ".". Is this correct? I have tried it with and without the ^ and the $, but I get the same results. Any ideas why my QString isn't being split into three pieces?

    Thanks!

  2. #2
    Join Date
    Sep 2010
    Posts
    45
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QRegExp Help

    Try this:

    QRegExp rx( "^([A-Z]{2,2}-[0-9]{6,6})(\\.[0-9]{1,4})?$" );

    Nb.
    the point is a regexp command, so for matching a point you must write \\.

    AB-123456 or CD-123456.78. As you can see, they all will begin with 2 letters, followed by a -, followed by 6 numbers. Then a "." with 1-4 numbers is optional. Currently, I have a QString that looks as follows: "AB-123456CD-123456.78EF-123456". I need to split this into its 3 parts: AB-123456, CD-123456.78, EF-123456. Here is my code. The QStringList returns a 1-item list with the whole string returned. Any ideas.
    Edit
    Try this:
    ^(\\w{2,2}\\-\\d{6,6})(\\w{2,2}\\-\\d{6,6})\\.(\\w{2,2}\\-\\d{6,6})$

    The parenthesis are the capturing operator.
    So you must uses the member funtion:
    http://doc.qt.nokia.com/4.7/qregexp.html#capturedTexts

    For capturing your text ...
    Last edited by bred; 15th November 2010 at 20:03.

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QRegExp Help

    Although we normally don't deliver completed codes, here we go:
    Qt Code:
    1. QString text = "AB-123456CD-123456.78EF-123456";
    2. QRegExp rx( "([A-Z]{2}-[0-9]{6}(?:\\.[0-9]{1,4})?)" );
    3.  
    4. int pos = 0;
    5. while ((pos = rx.indexIn(text, pos)) != -1) {
    6. list << rx.cap(0);
    7. pos += rx.matchedLength();
    8. }
    To copy to clipboard, switch view to plain text mode 
    First, if you use wildcard syntax only "*", "?" and "[]" are allowed, but you also have used "()". So it havn't worked. Also if you use reg exp. ^ and $ isn't what you want, because then your line is only allowed to have one of your pattern. Not more. To get an independent solution use the code above where it does not matter how often your pattern is found. Note also that with (?: foo bar ) you exclude the matched characters from the captured texts.

    So try to understand, what the code actually does, and please do not blind copy and paste...

  4. #4
    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: QRegExp Help

    I think the expression can be even simpler - without the look-ahead check. I didn't test it but it should work:
    Qt Code:
    1. QRegExp rx( "[A-Z]{2}-\\d{6}(\\.\\d{1,4})?" );
    To copy to clipboard, switch view to plain text mode 

    Then a while loop is employed as already suggested. But I would change the condition of the loop - pos should always be 0 with a positive match. Otherwise you'll be skipping characters which do not match instead of returning some parsing error.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Smile Re: QRegExp Help

    Quote Originally Posted by Lykurg View Post
    Qt Code:
    1. QString text = "AB-123456CD-123456.78EF-123456";
    2. QRegExp rx( "([A-Z]{2}-[0-9]{6}(?:\\.[0-9]{1,4})?)" );
    3.  
    4. int pos = 0;
    5. while ((pos = rx.indexIn(text, pos)) != -1) {
    6. list << rx.cap(0);
    7. pos += rx.matchedLength();
    8. }
    To copy to clipboard, switch view to plain text mode 
    Can you please explain what the
    Qt Code:
    1. ?:\\.
    To copy to clipboard, switch view to plain text mode 
    is trying to say.

    Also, why do you loop through the QRegExp and not just use the QString::split function that is already created?

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QRegExp Help

    ?: the reason why (...) is not captured.
    \\. escaping the . that it is taken literal and not in the meaning "any character"
    As to the split function: try to use it, and see why we use a loop.

  7. #7
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QRegExp Help

    QString::split uses matches of regular expression as separators not as founded strings!
    about: "\\." - first back slash escape from C++ special character in literal. First + Second back slash is single slash in final string and this is escape character in regular expression special character (dot in reg exp means "any character", in this case we want to have a real dot).

Similar Threads

  1. QRegExp
    By tebessum in forum Qt Programming
    Replies: 1
    Last Post: 3rd August 2008, 18:44
  2. QRegExp help
    By Lele in forum Qt Programming
    Replies: 2
    Last Post: 8th February 2008, 10:07
  3. QRegExp
    By szczav in forum Qt Programming
    Replies: 4
    Last Post: 19th June 2007, 20:07
  4. QRegExp Help
    By Ahmad in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2007, 00:13
  5. QRegExp?
    By Marco812 in forum Qt Programming
    Replies: 3
    Last Post: 4th August 2006, 08:31

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.