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. Regex sql Where condition
Forum Update on Monday, May 27th 2025

Regex sql Where condition

Scheduled Pinned Locked Moved Solved General and Desktop
7 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.
  • C Offline
    C Offline
    chilarai
    wrote on 5 Aug 2020, 07:54 last edited by chilarai 8 May 2020, 07:58
    #1

    What should be my Regex to match fetch the WHERE condition without (GROUP\\ BY|HAVING|ORDER\\ BY|ASC|DESC|LIMIT)? It works fine if I have any of the described params. It should also work for both the cases (with or without these parameters

    I need to get id=1 or id=2

    QString query = "SELECT * FROM users WHERE id=1 or id=2"
    QString whereString;  
    
    QRegularExpression whereListRegex("\\ WHERE\\ (.*?)\\ (GROUP\\ BY|HAVING|ORDER\\ BY|ASC|DESC|LIMIT)\\ ", QRegularExpression::CaseInsensitiveOption);
    QRegularExpressionMatch whereIterator = whereListRegex.match(query);
    
    J 1 Reply Last reply 5 Aug 2020, 08:00
    0
    • C chilarai
      5 Aug 2020, 07:54

      What should be my Regex to match fetch the WHERE condition without (GROUP\\ BY|HAVING|ORDER\\ BY|ASC|DESC|LIMIT)? It works fine if I have any of the described params. It should also work for both the cases (with or without these parameters

      I need to get id=1 or id=2

      QString query = "SELECT * FROM users WHERE id=1 or id=2"
      QString whereString;  
      
      QRegularExpression whereListRegex("\\ WHERE\\ (.*?)\\ (GROUP\\ BY|HAVING|ORDER\\ BY|ASC|DESC|LIMIT)\\ ", QRegularExpression::CaseInsensitiveOption);
      QRegularExpressionMatch whereIterator = whereListRegex.match(query);
      
      J Offline
      J Offline
      JonB
      wrote on 5 Aug 2020, 08:00 last edited by JonB 8 May 2020, 08:09
      #2

      @chilarai
      Looks like you want:

      QRegularExpression whereListRegex("\\ WHERE\\ (.*?)(\\ (GROUP\\ BY|HAVING|ORDER\\ BY|ASC|DESC|LIMIT)\\ )?", QRegularExpression::CaseInsensitiveOption);
      

      I don't know about all your backslashing and spaces. I also think what you have is not right/good enough. But the important thing here to answer your question is that in a reg ex

      (...)?
      

      means "0 or 1 occurrences of what is inside the parentheses to the left of the question mark", i.e. ? means preceding is optional.

      Just to be clear: you will not be able to robustly parse a SQL statement like this using regular expressions. I can easily come up with legitimate queries which it will mis-parse. Your approach will work if you have a limited number of cases you wish to support, so long as that OK in the context where you want to use this.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        chilarai
        wrote on 5 Aug 2020, 08:12 last edited by
        #3

        @JonB said in Regex sql Where condition:

        ("\ WHERE\ (.*?)(\ (GROUP\ BY|HAVING|ORDER\ BY|ASC|DESC|LIMIT)\ )?",

        Your solution is not working.

        Also, if what I am doing is wrong can you point me to the right way to matching the conditions

        J 1 Reply Last reply 5 Aug 2020, 08:51
        0
        • C chilarai
          5 Aug 2020, 08:12

          @JonB said in Regex sql Where condition:

          ("\ WHERE\ (.*?)(\ (GROUP\ BY|HAVING|ORDER\ BY|ASC|DESC|LIMIT)\ )?",

          Your solution is not working.

          Also, if what I am doing is wrong can you point me to the right way to matching the conditions

          J Offline
          J Offline
          JonB
          wrote on 5 Aug 2020, 08:51 last edited by JonB 8 May 2020, 09:07
          #4

          @chilarai
          My answer will work based on what you had. However, as I said, if that is not good enough/wrong (which I see as the case) it will not improve the situation. For example, you have specified your "optional group" as ending in \\ ). That would mean it can only match (be optional) if there is that space at the end. If you don't have that, it won't match.

          As a by-the-by, there is no need to protect the spaces with \, and it just makes your expression harder to read. I think

          " WHERE (.*?) (GROUP BY|HAVING|ORDER BY|ASC|DESC|LIMIT) "
          

          would do the same as what you presently have.

          You might simplify your current to something more like:

          " WHERE (.*)( GROUP|ORDER|LIMIT)?"
          

          [EDIT: Hmm, that may not work, it should work for the plain WHERE id=1 or id=2 case; but I think the (.*) will include any e.g. GROUP ..., and leave the ( GROUP|ORDER|LIMIT)? capture group empty, which is not what you want.... This will require a more advanced reg ex, with some "not"-ting... See my adjustment in my next post below.]

          I am not going to go through all the possibilities of what you might want here. That is for you to do, I suggest you go to a site like https://regex101.com/ to test your expression as you design.

          However, I have said that no regular expression will correctly parse a SQL condition. So nobody can tell you what to write, until you define what limited subset of SQL you wish to support.

          C 1 Reply Last reply 5 Aug 2020, 08:59
          1
          • J JonB
            5 Aug 2020, 08:51

            @chilarai
            My answer will work based on what you had. However, as I said, if that is not good enough/wrong (which I see as the case) it will not improve the situation. For example, you have specified your "optional group" as ending in \\ ). That would mean it can only match (be optional) if there is that space at the end. If you don't have that, it won't match.

            As a by-the-by, there is no need to protect the spaces with \, and it just makes your expression harder to read. I think

            " WHERE (.*?) (GROUP BY|HAVING|ORDER BY|ASC|DESC|LIMIT) "
            

            would do the same as what you presently have.

            You might simplify your current to something more like:

            " WHERE (.*)( GROUP|ORDER|LIMIT)?"
            

            [EDIT: Hmm, that may not work, it should work for the plain WHERE id=1 or id=2 case; but I think the (.*) will include any e.g. GROUP ..., and leave the ( GROUP|ORDER|LIMIT)? capture group empty, which is not what you want.... This will require a more advanced reg ex, with some "not"-ting... See my adjustment in my next post below.]

            I am not going to go through all the possibilities of what you might want here. That is for you to do, I suggest you go to a site like https://regex101.com/ to test your expression as you design.

            However, I have said that no regular expression will correctly parse a SQL condition. So nobody can tell you what to write, until you define what limited subset of SQL you wish to support.

            C Offline
            C Offline
            chilarai
            wrote on 5 Aug 2020, 08:59 last edited by
            #5

            @JonB Thanks for the suggestion

            J 1 Reply Last reply 5 Aug 2020, 09:07
            0
            • C chilarai
              5 Aug 2020, 08:59

              @JonB Thanks for the suggestion

              J Offline
              J Offline
              JonB
              wrote on 5 Aug 2020, 09:07 last edited by
              #6

              @chilarai
              It seems to me: you want to "stop at" any GROUP|ORDER|LIMIT. I think your original (.*?) --- "non-greedy" --- was correct. So my "very simple" suggestion would indeed be:

              " WHERE (.*?)( GROUP|ORDER|LIMIT)?"
              
              C 1 Reply Last reply 5 Aug 2020, 15:16
              0
              • J JonB
                5 Aug 2020, 09:07

                @chilarai
                It seems to me: you want to "stop at" any GROUP|ORDER|LIMIT. I think your original (.*?) --- "non-greedy" --- was correct. So my "very simple" suggestion would indeed be:

                " WHERE (.*?)( GROUP|ORDER|LIMIT)?"
                
                C Offline
                C Offline
                chilarai
                wrote on 5 Aug 2020, 15:16 last edited by
                #7

                @JonB Thanks. With your help and some others, I have finally narrowed down my query to

                QRegularExpression whereListRegex(R"(\sWHERE\s+(.*?)(?:\s+(?:GROUP|ORDER|LIMIT)\b|\s*$))", QRegularExpression::CaseInsensitiveOption);
                

                This works for both the conditions I have asked for

                1 Reply Last reply
                2

                3/7

                5 Aug 2020, 08:12

                • Login

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