Results 1 to 2 of 2

Thread: Negative regexp

  1. #1
    Join Date
    Sep 2009
    Location
    Warsaw/Poland
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Negative regexp

    Hello,

    I have problem with negative regexp in QT,
    I've want capture string after "//"
    example: in "//aaa" - i want "aaa"
    but I dont want capture when before "//" is http: or https:

    I've tried this 4 regexp, but all are wrong ...

    please help

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QRegExp>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8.  
    9. QRegExp rx1("(ftp:|http:|https:){0}//([a-zA-Z0-9_-]+)");
    10. QRegExp rx2("[^(ftp:|http:|https:)]//([a-zA-Z0-9_-]+)");
    11. QRegExp rx3("(?=(ftp:|http:|https:))//([a-zA-Z0-9_-]+)");
    12. QRegExp rx4("(?!(ftp:|http:|https:))//([a-zA-Z0-9_-]+)");
    13.  
    14. QString strData;
    15. strData = "//aaa";
    16.  
    17. // should be != -1
    18. qDebug() << "1:" << rx1.indexIn(strData) << " " << (rx1.indexIn(strData) != -1 ? "OK" : "WRONG");
    19. qDebug() << "2:" << rx2.indexIn(strData) << " " << (rx2.indexIn(strData) != -1 ? "OK" : "WRONG");
    20. qDebug() << "3:" << rx3.indexIn(strData) << " " << (rx3.indexIn(strData) != -1 ? "OK" : "WRONG");
    21. qDebug() << "4:" << rx4.indexIn(strData) << " " << (rx4.indexIn(strData) != -1 ? "OK" : "WRONG");
    22.  
    23. qDebug() << "";
    24. strData = "http://aaa";
    25.  
    26. // should be == -1
    27. qDebug() << "1:" << rx1.indexIn(strData) << " " << (rx1.indexIn(strData) == -1 ? "OK" : "WRONG");
    28. qDebug() << "2:" << rx2.indexIn(strData) << " " << (rx2.indexIn(strData) == -1 ? "OK" : "WRONG");
    29. qDebug() << "3:" << rx3.indexIn(strData) << " " << (rx3.indexIn(strData) == -1 ? "OK" : "WRONG");
    30. qDebug() << "4:" << rx4.indexIn(strData) << " " << (rx4.indexIn(strData) == -1 ? "OK" : "WRONG");
    31.  
    32. // rx.cap ... etc
    33.  
    34. return a.exec();
    35. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2012
    Location
    Montreal
    Posts
    14
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Negative regexp

    ?! is the negative lookahead operator, it doesn't work for stuff before. What you can do is to have a non-capturing group to define a group that starts the line with ^https?://.*

    I haven't tested it, but it should look like:

    Qt Code:
    1. QRegExp rx("(?:^(?:ftp|http|https)://.*$)?//(\\w+)");
    To copy to clipboard, switch view to plain text mode 

    I am not sure of the usage of a non-capturing group within a non-capturing group. You might need to use (?:ftp://.*|http://.*|https://.*) instead.

    - Maxime

Similar Threads

  1. SQLITE and REGEXP
    By miraks in forum Qt Programming
    Replies: 3
    Last Post: 16th March 2011, 21:55
  2. RegExp Question
    By slava in forum Qt Programming
    Replies: 3
    Last Post: 12th July 2010, 19:33
  3. RegExp
    By pucara_faa in forum Qt Programming
    Replies: 2
    Last Post: 15th November 2009, 13:41
  4. RegExp question
    By high_flyer in forum General Programming
    Replies: 1
    Last Post: 26th August 2007, 18:23
  5. Quick RegExp question
    By stealth86 in forum Qt Programming
    Replies: 3
    Last Post: 26th July 2007, 08:23

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.