Results 1 to 5 of 5

Thread: qregexp can't get match 'not preceeded by' to work

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default qregexp can't get match 'not preceeded by' to work

    Hi I can't figure out the regex to match any number not preceded by '['

    I don't know why this doesn't work:

    QRegExp n( "(?![)[0-9]+" );

    I could also get the match I want by searching for a number not followed by ']' but I can't get that regex to work either:

    QRegExp n( "[0-9]+(?!])" );

    I have tried many variations of this, including escaping the ]'s like this --> "(?!\\])" --- but i can't get my match.

    Just to be clear I want to match 150 but not [150].

    Also, I would use "[^[]" but that would not match a number at the very beginning of the line.

    What am I missing?
    Thanks.
    Last edited by kja; 25th July 2011 at 02:11.

  2. #2
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qregexp can't get match 'not preceeded by' to work

    Hi, try this code:
    Qt Code:
    1. QRegExp rx1("\\[(\\d+)\\]");
    2. QString str = "Some text [12] and [14] numbers 99 [231] 7";
    3. int pos = 0;
    4.  
    5. while ((pos = rx1.indexIn(str, pos)) != -1)
    6. {
    7. list << rx1.cap(1);
    8. pos += rx1.matchedLength();
    9. }
    10.  
    11. for (int i=0; i<list.size(); i++)
    12. {
    13. qDebug()<<list[i];
    14. }
    To copy to clipboard, switch view to plain text mode 
    In console output you get:
    "12"
    "14"
    "231"

    --
    Sorry for my bad English
    Last edited by NortT; 25th July 2011 at 04:49.

  3. The following user says thank you to NortT for this useful post:

    kja (25th July 2011)

  4. #3
    Join Date
    Oct 2010
    Posts
    58
    Thanks
    26
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qregexp can't get match 'not preceeded by' to work

    Thanks for your help, I realized I was not clear with my question. To use your example

    QString str = "Some text [12] and [14] numbers 99 [231] 7";

    I would only want to match 99 and 7, not any of the numbers within the brackets.

    I want to figure out how to use the 'not preceded by' or 'not followed by' regexp's

    The QregExp documentation (http://doc.qt.nokia.com/latest/qregexp.html) talks about negative lookahead syntax but the same doesn't seem to work for looking behind, but I have gotten a little closer:

    QRegExp n("[0-9]+(?!\\])");

    string = "blah [10] 24"
    matches 1 and 24.

    This matches the '1' in the [10], which I don't want, but (?!\\[) put in front does not work...

    any ideas?





    Thanks.
    Last edited by kja; 25th July 2011 at 18:44.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: qregexp can't get match 'not preceeded by' to work

    The QRegExp docs talk about look behind assertions:
    Both zero-width positive and zero-width negative lookahead assertions (?=pattern) and (?!pattern) are supported with the same syntax as Perl. Perl's lookbehind assertions, "independent" subexpressions and conditional expressions are not supported.
    This the subject of a few bug reports.

  6. The following user says thank you to ChrisW67 for this useful post:

    kja (26th July 2011)

  7. #5
    Join Date
    Apr 2007
    Location
    Ilsfeld, Germany
    Posts
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qregexp can't get match 'not preceeded by' to work

    Hi,

    the RegExp machine always tries to make a match.

    In "blah [10] 24" the "[0-9]+" matches the "1" and the "(?!\\])" matches the "0".
    This is correct because "0" is not "]".

    To prevent the RE here to match the "0" you either need a possessive quantifier (like (?>) in Perl or ++ in Java, but sadly not available in QRegExp) or you also need to exclude the digits in your lookahead. Use "(?![]0-9])" here.

    Because there also exists no lookbehind you need a "[^[0-9]" to exclude a leading "[". To also match at the beginning of the string you need to add a "^|" in the beginning.

    Note that now the character before the 'wanted match' is part of the match itself, e.g. you get " 99", not only "99".

    Where required capture the digits using () and use .cap(1).

    HTH, Bernd
    --
    Qt Code:
    1. int main(void)
    2. {
    3. QString str = "Some text [12] and [14] numbers 99 [231] 7";
    4. QRegExp rx("(?:^|[^[0-9])([0-9]+)(?![]0-9])");
    5.  
    6. int pos = 0;
    7. while ((pos = rx.indexIn(str, pos)) != -1)
    8. {
    9. qDebug() << rx.cap(1);
    10. pos += rx.matchedLength();
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Multiline match with QRegExp
    By bender86 in forum Qt Programming
    Replies: 5
    Last Post: 27th November 2016, 15:14
  2. Replies: 1
    Last Post: 10th March 2010, 18:19
  3. Replies: 1
    Last Post: 21st September 2009, 07:30
  4. QRegExp match all excpet "_"
    By mattia in forum Newbie
    Replies: 6
    Last Post: 28th March 2008, 12:53
  5. QRegExp doesn't seem to work as it should
    By thomaspu in forum Qt Programming
    Replies: 3
    Last Post: 21st December 2007, 07: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
  •  
Qt is a trademark of The Qt Company.