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. Question on using QRegularExpression
Forum Updated to NodeBB v4.3 + New Features

Question on using QRegularExpression

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 548 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
    #1

    Hi!

    I read a bunch of posts and read the docs but couldn't seem to find an answer to the issue I have.
    In Qt 5 we were able to use QRegExp with a code fragment shown below
    QRegularExpression digits("\d+"); // '\d+' (one or more digits)
    QStringList attributeValues;

    for(int i = 0; i < attributeValues.size(); i++)
    {
    if(digits.exactMatch(attributeValues[i]) && !attributeValues[i].contains(".")) //make sure floating point numbers are not included
    {
    intAttributeValues.append(attributeValues[i].toInt());
    }
    }

    I'd like to implement the same logic using QRegularExpression
    I know I can do something like this:
    QRegularExpression digits(QRegularExpression::anchoredPattern("\d+"));

    But how do implement an exact match as above with the if statement?

    Any help would be appreciated!

    L 1 Reply Last reply
    0
    • JonBJ JonB

      @leinad said in Question on using QRegularExpression:

      but how do I check if my variable has attributeValues[i] has digits?

      Sorry, I wrote the idea earlier but not the exact correct thing! Should be:

      QRegularExpression digits(QRegularExpression::anchoredPattern("\\d+"));
      for(int i = 0; i < attributeValues.size(); i++)
          if (digits.match(attributeValues[i]).hasMatch())
      

      Is that what you mean?

      L Offline
      L Offline
      leinad
      wrote on last edited by
      #8

      @JonB Right, that makes sense. I think what I have is the same I just decided to add a boolean for simplicity :)

      1 Reply Last reply
      1
      • L leinad

        Hi!

        I read a bunch of posts and read the docs but couldn't seem to find an answer to the issue I have.
        In Qt 5 we were able to use QRegExp with a code fragment shown below
        QRegularExpression digits("\d+"); // '\d+' (one or more digits)
        QStringList attributeValues;

        for(int i = 0; i < attributeValues.size(); i++)
        {
        if(digits.exactMatch(attributeValues[i]) && !attributeValues[i].contains(".")) //make sure floating point numbers are not included
        {
        intAttributeValues.append(attributeValues[i].toInt());
        }
        }

        I'd like to implement the same logic using QRegularExpression
        I know I can do something like this:
        QRegularExpression digits(QRegularExpression::anchoredPattern("\d+"));

        But how do implement an exact match as above with the if statement?

        Any help would be appreciated!

        L Offline
        L Offline
        leinad
        wrote on last edited by
        #2

        @leinad
        I think I figure it out. Can someone please confirm if it makes sense?
        QStringList attributeValues;
        QRegularExpression digits(QRegularExpression::anchoredPattern("\d+"));

        for(int i = 0; i < attributeValues.size(); i++)
        {
        QRegularExpressionMatch match = digits.match(attributeValues[i]);
        bool intMatch = match.hasMatch();
        if(intMatch && !attributeValues[i].contains("."))
        {
        intAttributeValues.append(attributeValues[i].toInt());
        }
        }

        JonBJ 1 Reply Last reply
        0
        • L leinad

          @leinad
          I think I figure it out. Can someone please confirm if it makes sense?
          QStringList attributeValues;
          QRegularExpression digits(QRegularExpression::anchoredPattern("\d+"));

          for(int i = 0; i < attributeValues.size(); i++)
          {
          QRegularExpressionMatch match = digits.match(attributeValues[i]);
          bool intMatch = match.hasMatch();
          if(intMatch && !attributeValues[i].contains("."))
          {
          intAttributeValues.append(attributeValues[i].toInt());
          }
          }

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #3

          @leinad
          It would be really helpful, for me at least, if you put blocks of code inside forum Code tags ( ``` ) .

          1 Reply Last reply
          1
          • L Offline
            L Offline
            leinad
            wrote on last edited by
            #4

            I tried to figure out how to do that but couldn't find a way. Is this better?
            I assume what I did makes sense?
            Thanks!

            QStringList attributeValues;
            QRegularExpression digits(QRegularExpression::anchoredPattern("\d+"));
            
            for(int i = 0; i < attributeValues.size(); i++)
            {
                QRegularExpressionMatch match = digits.match(attributeValues[i]);
                bool intMatch = match.hasMatch();
                if(intMatch && !attributeValues[i].contains("."))
                {
                    intAttributeValues.append(attributeValues[i].toInt());
                }
            }
            
            JonBJ 1 Reply Last reply
            0
            • L leinad

              I tried to figure out how to do that but couldn't find a way. Is this better?
              I assume what I did makes sense?
              Thanks!

              QStringList attributeValues;
              QRegularExpression digits(QRegularExpression::anchoredPattern("\d+"));
              
              for(int i = 0; i < attributeValues.size(); i++)
              {
                  QRegularExpressionMatch match = digits.match(attributeValues[i]);
                  bool intMatch = match.hasMatch();
                  if(intMatch && !attributeValues[i].contains("."))
                  {
                      intAttributeValues.append(attributeValues[i].toInt());
                  }
              }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #5

              @leinad
              Firstly, you know that your "\d+" is wrong in both cases (you may be getting away with it), it should always be "\\d+".

              As you say: if (regExp.exactMatch()) needs to be translated to if (regularExpression.match(QRegularExpression::anchoredPattern("\\d+")).hasMatch()).

              L 1 Reply Last reply
              0
              • JonBJ JonB

                @leinad
                Firstly, you know that your "\d+" is wrong in both cases (you may be getting away with it), it should always be "\\d+".

                As you say: if (regExp.exactMatch()) needs to be translated to if (regularExpression.match(QRegularExpression::anchoredPattern("\\d+")).hasMatch()).

                L Offline
                L Offline
                leinad
                wrote on last edited by leinad
                #6

                @JonB Thanks, I had a typo when I typed the code into this page. I do indeed have "\\d+"
                In your logic you have
                if (regularExpression.match(QRegularExpression::anchoredPattern("\\d+")).hasMatch())
                but how do I check if my variable has attributeValues[i] has digits? It looks like my logic is correct I just broke it down into smaller code segments.

                JonBJ 1 Reply Last reply
                0
                • L leinad

                  @JonB Thanks, I had a typo when I typed the code into this page. I do indeed have "\\d+"
                  In your logic you have
                  if (regularExpression.match(QRegularExpression::anchoredPattern("\\d+")).hasMatch())
                  but how do I check if my variable has attributeValues[i] has digits? It looks like my logic is correct I just broke it down into smaller code segments.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

                  @leinad said in Question on using QRegularExpression:

                  but how do I check if my variable has attributeValues[i] has digits?

                  Sorry, I wrote the idea earlier but not the exact correct thing! Should be:

                  QRegularExpression digits(QRegularExpression::anchoredPattern("\\d+"));
                  for(int i = 0; i < attributeValues.size(); i++)
                      if (digits.match(attributeValues[i]).hasMatch())
                  

                  Is that what you mean?

                  L 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @leinad said in Question on using QRegularExpression:

                    but how do I check if my variable has attributeValues[i] has digits?

                    Sorry, I wrote the idea earlier but not the exact correct thing! Should be:

                    QRegularExpression digits(QRegularExpression::anchoredPattern("\\d+"));
                    for(int i = 0; i < attributeValues.size(); i++)
                        if (digits.match(attributeValues[i]).hasMatch())
                    

                    Is that what you mean?

                    L Offline
                    L Offline
                    leinad
                    wrote on last edited by
                    #8

                    @JonB Right, that makes sense. I think what I have is the same I just decided to add a boolean for simplicity :)

                    1 Reply Last reply
                    1
                    • L leinad has marked this topic as solved on

                    • Login

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