Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Trying to use QregularExpression
QtWS25 Last Chance

Trying to use QregularExpression

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    leinad
    wrote on last edited by Chris Kawa
    #1

    Hi,

    I migrated to Qt 6.2.4 and now found that QRegExp has been deprecated and we should now use QRegularExpression.

    I have code using QRegExp which worked fine but when I try to replace it with a QRegularExpression it does not return the correct results. In otherwords not all the IPs are returned

    I have a QString which contains multiple IPs and I would like to extract all the IPs from the string. Some may repeat and some may not.

    This code works fine using QRegExp:

    QRegExp rx("(((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))");
    QStringList initialList ;
        while ((pos = rx.indexIn(msg, pos)) != -1)
        {
            initialList << rx.cap(1); 
            pos += rx.matchedLength();
        } 
    

    When I try to do the following using QRegularExpression it does not return all the IP's.

    static QRegularExpression ipRX("(((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))");
    QRegularExpressionMatch match = ipRX.match(msg);
    QStringList initialList ;
        if(match.hasMatch())
        {
            initialList << match.capturedTexts();
            initialList.removeDuplicates();
        }
    

    Any ideas what I'm doing wrong?

    Thanks.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      QRegularExpression::match only looks for the first match. capturedTexts() returns the capture groups in that single match.

      A code equivalent to the QRegExp one would be something like this:

      QRegularExpressionMatchIterator i = ipRX.globalMatch(msg);
      while(i.hasNext())
      {
          initialList << i.next().captured(1);
      }
      
      1 Reply Last reply
      1
      • L Offline
        L Offline
        leinad
        wrote on last edited by
        #3

        Great! Thanks so much.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved