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. A question about regex usage using Qt library
Forum Updated to NodeBB v4.3 + New Features

A question about regex usage using Qt library

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 528 Views 1 Watching
  • 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.
  • H Offline
    H Offline
    HFT_developer
    wrote on 18 Dec 2022, 11:18 last edited by HFT_developer
    #1

    Hi,

    I have managed to create a regex that can be used to verify polynomials. My algorithm to verify the polynomial is that if the number of matches and the size of the length of the polynomial are the same that poly must be valid.

    Using C++ STL, I could do something like:

    std::string sample1 = {"4x^3 - 9x^2 + 10x^1 -5x^0 -45x^-1"}; // valid poly 
    std::string sample2 = {"4x^3 - 9x^2 + 10x -5 -45x^-1"};// invalid poly
    
    //  I removed all the white space from the input string 
    bool verifyPolynomial(const std::string& poly){
    
        std::smatch matches;
        
        std::regex _regex("(-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+");// this is tested on regex101 website
        
        std::regex_search(poly, matches, _regex);
        
        return (matches.size() == poly.size() );
    }
    

    Link: https://regex101.com/r/D7HjR7/1

    How do I convert this into Qt equivalent code ?

    I have something like this so far:

    QRegularExpression Gui_app_handler::m_regex2 = QRegularExpression("(-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+");
    // this function crashes the app
    bool Gui_app_handler::verifyPolynomial(const QString &polynomial){
    
        int _matches{};
        QRegularExpressionMatchIterator it = m_regex2.globalMatch(polynomial);
        while (it.hasNext() ){
            ++_matches;
        }
    
        return (_matches == polynomial.size());
    }
    
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 18 Dec 2022, 11:34 last edited by
      #2

      Your while loop never advances the iterator, by calling next(), so "crashes the app" is probably an endless, CPU intensive loop.

      H 1 Reply Last reply 18 Dec 2022, 11:55
      4
      • C ChrisW67
        18 Dec 2022, 11:34

        Your while loop never advances the iterator, by calling next(), so "crashes the app" is probably an endless, CPU intensive loop.

        H Offline
        H Offline
        HFT_developer
        wrote on 18 Dec 2022, 11:55 last edited by
        #3

        @ChrisW67 ok thanks. Actually how can I convert the regex101 to C++ regex, because this gives warning:

        // this one gives warning from: https://regex101.com/r/D7HjR7/1
        QRegularExpression Gui_app_handler::m_regex2 = QRegularExpression("(-|\+)?[0-9]+[a-z]\^(-|\+)?[0-9]+");
        
        
        QRegularExpression Gui_app_handler::m_regex2 = QRegularExpression("(-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+");// I removed the warning but this is not matching as expected as regex 101
        
        bool Gui_app_handler::verifyPolynomial(const QString &polynomial){
        
            int _matches{};
            QRegularExpressionMatchIterator it = m_regex2.globalMatch(polynomial);
            while (it.hasNext() ){
                it.next();
                ++_matches;
            }
        
            return (_matches == polynomial.size());
        }
        
        
        J 1 Reply Last reply 18 Dec 2022, 14:01
        0
        • H HFT_developer
          18 Dec 2022, 11:55

          @ChrisW67 ok thanks. Actually how can I convert the regex101 to C++ regex, because this gives warning:

          // this one gives warning from: https://regex101.com/r/D7HjR7/1
          QRegularExpression Gui_app_handler::m_regex2 = QRegularExpression("(-|\+)?[0-9]+[a-z]\^(-|\+)?[0-9]+");
          
          
          QRegularExpression Gui_app_handler::m_regex2 = QRegularExpression("(-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+");// I removed the warning but this is not matching as expected as regex 101
          
          bool Gui_app_handler::verifyPolynomial(const QString &polynomial){
          
              int _matches{};
              QRegularExpressionMatchIterator it = m_regex2.globalMatch(polynomial);
              while (it.hasNext() ){
                  it.next();
                  ++_matches;
              }
          
              return (_matches == polynomial.size());
          }
          
          
          J Offline
          J Offline
          JonB
          wrote on 18 Dec 2022, 14:01 last edited by JonB
          #4

          @HFT_developer said in A question about regex usage using Qt library:

          because this gives warning:

          What warning? A warning whose text you don't show but you'd like people to guess and fix?

          // I removed the warning but this is not matching as expected as regex 101

          And what is it doing?

          H 1 Reply Last reply 18 Dec 2022, 17:25
          0
          • J JonB
            18 Dec 2022, 14:01

            @HFT_developer said in A question about regex usage using Qt library:

            because this gives warning:

            What warning? A warning whose text you don't show but you'd like people to guess and fix?

            // I removed the warning but this is not matching as expected as regex 101

            And what is it doing?

            H Offline
            H Offline
            HFT_developer
            wrote on 18 Dec 2022, 17:25 last edited by
            #5

            @JonB The warning was about unknown escape sequence which is \+ but should be \\+. So I fixed it:

            QRegularExpression Gui_app_handler::m_regex2 = QRegularExpression("((-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+)+");
            

            So using STL I've got it to work:

            bool verifyPolynomial(const std::string& poly){
                std::regex _regex("((-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+)+");
                return regex_match(poly, _regex);
            }
            
            
            int main()
            {
                std::string sample1 = {"4x^3-9x^2+10x^1-5x^0-45x^-1"}; // should be valid 
                std::string sample2 = {"4x^3-9x^2+10x^1-5x^0-45x^"};// should be invalid 
                
                std::cout<<"verifyPolynomial1: "<<verifyPolynomial(sample1)<<"\n";
                std::cout<<"verifyPolynomial2: "<<verifyPolynomial(sample2)<<"\n";
            }
            

            For Qt I did this:

            bool Gui_app_handler::verifyPolynomial(const QString &polynomial){
                QRegularExpressionMatch match = m_regex2.match(polynomial); 
                return match.hasMatch();
            }
            // but this function is returning true and for wrong stuff like "4x^3-9x^2+10x^1-5x^0-45x^"
            

            Why is the Qt one not functioning the same as STL?

            C 1 Reply Last reply 18 Dec 2022, 22:27
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 18 Dec 2022, 19:34 last edited by SGaist
              #6

              Hi,

              As silly as it may sound: because Qt != STL.

              Note that Qt had regular expressions classes way before std did. So the question could be asked the other way around, why does std not behave like Qt ?

              Anyway, the goal here is not to start a flamewar. If you take the time to do some spelunking with regard to Qt and the STL, you will see that many things you find now in the std have origins in Qt.

              There will always be differences though but both can coexist.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • H HFT_developer
                18 Dec 2022, 17:25

                @JonB The warning was about unknown escape sequence which is \+ but should be \\+. So I fixed it:

                QRegularExpression Gui_app_handler::m_regex2 = QRegularExpression("((-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+)+");
                

                So using STL I've got it to work:

                bool verifyPolynomial(const std::string& poly){
                    std::regex _regex("((-|\\+)?[0-9]+[a-z]\\^(-|\\+)?[0-9]+)+");
                    return regex_match(poly, _regex);
                }
                
                
                int main()
                {
                    std::string sample1 = {"4x^3-9x^2+10x^1-5x^0-45x^-1"}; // should be valid 
                    std::string sample2 = {"4x^3-9x^2+10x^1-5x^0-45x^"};// should be invalid 
                    
                    std::cout<<"verifyPolynomial1: "<<verifyPolynomial(sample1)<<"\n";
                    std::cout<<"verifyPolynomial2: "<<verifyPolynomial(sample2)<<"\n";
                }
                

                For Qt I did this:

                bool Gui_app_handler::verifyPolynomial(const QString &polynomial){
                    QRegularExpressionMatch match = m_regex2.match(polynomial); 
                    return match.hasMatch();
                }
                // but this function is returning true and for wrong stuff like "4x^3-9x^2+10x^1-5x^0-45x^"
                

                Why is the Qt one not functioning the same as STL?

                C Offline
                C Offline
                ChrisW67
                wrote on 18 Dec 2022, 22:27 last edited by
                #7

                @HFT_developer said in A question about regex usage using Qt library:

                Why is the Qt one not functioning the same as STL?

                The secret is in the manual.

                STL std::regex_match() attempts to match a regular expression to an entire string

                Qt QRegularExpression::match() matches the pattern anywhere in the string (after the offset if provided).
                4x^3-9x^2+10x^1-5x^0-45x is matched because the RE matches 4x^3-9x^2+10x^1-5x^0
                QRegularExpression will honour the ^ and/or $ anchors if present, so you can use those to match on a whole string.

                1 Reply Last reply
                0
                • JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on 18 Dec 2022, 22:44 last edited by
                  #8

                  you may be able to save some time if you use equation passer libs.
                  https://beltoforion.de/en/muparser/

                  1 Reply Last reply
                  0

                  1/8

                  18 Dec 2022, 11:18

                  • Login

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