Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. how to use regex to verify a certain string pattern?
Forum Updated to NodeBB v4.3 + New Features

how to use regex to verify a certain string pattern?

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 4 Posters 1.1k Views 2 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 11 Dec 2022, 17:45 last edited by HFT_developer 12 Nov 2022, 18:58
    #1

    Hi,
    My aim to check whether a user has entered the input correctly. The input is a std::string which should be a polynomial. The following are two examples of what should be valid or invalid:

        std::string sample1 = {"4x^3 - 9x^2 + 10x^1 -5x^0 -45x^-1"}; // valid
        std::string sample2 = {"4x^3 - 9x^2 + 10x -5 -45x^-1"};// invalid
    

    Right now I am trying to use std::regex to verify this:

    bool verifyPolynomial(const std::string& poly){
         // Used when your searching a string
        std::smatch matches;
        
        std::regex _regex("[0-9]+[a-z]+\\^[0-9]");// I don't think this is correct?
        
        std::regex_search(poly, matches, _regex);
        
        return matches.size();
    }
    

    I am learning regex, so I find this a bit confusing if someone can help that would be great

    J 1 Reply Last reply 11 Dec 2022, 19:19
    0
    • H HFT_developer
      11 Dec 2022, 17:45

      Hi,
      My aim to check whether a user has entered the input correctly. The input is a std::string which should be a polynomial. The following are two examples of what should be valid or invalid:

          std::string sample1 = {"4x^3 - 9x^2 + 10x^1 -5x^0 -45x^-1"}; // valid
          std::string sample2 = {"4x^3 - 9x^2 + 10x -5 -45x^-1"};// invalid
      

      Right now I am trying to use std::regex to verify this:

      bool verifyPolynomial(const std::string& poly){
           // Used when your searching a string
          std::smatch matches;
          
          std::regex _regex("[0-9]+[a-z]+\\^[0-9]");// I don't think this is correct?
          
          std::regex_search(poly, matches, _regex);
          
          return matches.size();
      }
      

      I am learning regex, so I find this a bit confusing if someone can help that would be great

      J Offline
      J Offline
      JonB
      wrote on 11 Dec 2022, 19:19 last edited by
      #2

      @HFT_developer
      This is not an easy one for a beginner! Try playing with https://regex101.com, where you can test regular expressions against whatever input you give it.

      The final answer depends on how strict you want to be, what you do/do not want to allow, whether it must match the whole, complete string, how you want to handle whitespace, and so on.

      I see your attempt as close:

      [0-9]+[a-z]+\^-?[0-9]+
      

      might do you for each polynomial in the expression.

      Then you may have to go:

      [0-9]+[a-z]+\^-?[0-9]+(\s*[+-]\s*[0-9]+[a-z]+\^-?[0-9]+)*
      

      to do the series of polys separated by a + or - symbol. Or something like that :)

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 11 Dec 2022, 21:11 last edited by
        #3

        Hi,

        Qt also have QRegularExpression for that kind of work and there's a helper tool that you can build to test your expressions.

        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
        • K Offline
          K Offline
          Kent-Dorfman
          wrote on 12 Dec 2022, 20:45 last edited by
          #4

          mathematically speaking both expressions are valid. -5x^0 evalutates to -5 so the term is the same even if it is expressed differently.

          1 Reply Last reply
          0

          1/4

          11 Dec 2022, 17:45

          • Login

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