Results 1 to 2 of 2

Thread: Regular Expression using QRegularExpression sometimes failed

  1. #1
    Join Date
    Oct 2013
    Location
    South Ouest France
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Regular Expression using QRegularExpression sometimes failed

    Hello.

    Here is the code I use to extract a sub-string of unknown length. This character sub-string begins with the sequence code= and ends with a space.

    Qt Code:
    1. QString town_code;
    2. QRegularExpression re1("code=(.+)\\s");
    3. QRegularExpressionMatch match = re1.match (a_text);
    4. hasMatch = match.hasMatch();
    5. if (hasMatch) {
    6. town_code = match.captured(1);
    7. ............
    8. ............
    9. ............
    10. }
    To copy to clipboard, switch view to plain text mode 

    The result is correct for variable a_text containing:
    ARANTZAZU (OÑATI) code=onati-id20059 ville=OÑATI
    The town_code variable contains the correct value: onati-id20059


    The result is incorrect for variable a_text containing:
    OTSAURTE (ZEGAMA) code=Zegama-id20025 ville=ZEGAMA tameteo=http://www.tameteo.com/meteo_Zegama-Europe-Espagne-Guipuscoa--1-3385.html
    The town_code variable contains the incorrect value: Zegama-id20025 ville=ZEGAMA


    Help is welcome.

  2. #2
    Join Date
    Oct 2013
    Location
    South Ouest France
    Posts
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Solved : Regular Expression using QRegularExpression sometimes failed

    Hello.

    From unix.com
    Originally Posted by CarloM
    Rather than using (.+) to match any character, try using ([^\\s]+) to match non-whitespace characters (as in the earlier sed suggestions).
    Qt Code:
    1. QRegularExpression re1("code=(.+)\\s");
    To copy to clipboard, switch view to plain text mode 

    becomes :

    Qt Code:
    1. QRegularExpression re1("code=([^\\s]+)");
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Regular expression
    By aguleo in forum Newbie
    Replies: 0
    Last Post: 25th January 2013, 15:00
  2. Regular expression
    By QFreeCamellia in forum Newbie
    Replies: 8
    Last Post: 30th December 2011, 22:34
  3. Regular expression help!
    By ConkerX in forum Qt Programming
    Replies: 10
    Last Post: 31st August 2011, 15:47
  4. Help with regular expression
    By Gourmet in forum General Programming
    Replies: 19
    Last Post: 11th August 2011, 15:04
  5. Find with Regular Expression?
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2007, 14:44

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.