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. How to create a regular expression for any string up to a maximum length?
QtWS25 Last Chance

How to create a regular expression for any string up to a maximum length?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.6k 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.
  • G Offline
    G Offline
    Guerrian
    wrote on 25 May 2018, 18:20 last edited by
    #1

    This is what I tried to allow any string up to a maximum of 32 characters, but it does not work:

        QRegularExpression regExp("^.{,32}$");
    

    In my same code this expression worked fine:

        QRegularExpression regExp("^[0-9]{0,8}$");
    

    Linux Mint 18.3
    Qt 5.14.1
    Qt Creator 4.11.1

    R A 2 Replies Last reply 25 May 2018, 18:26
    1
    • G Guerrian
      25 May 2018, 18:20

      This is what I tried to allow any string up to a maximum of 32 characters, but it does not work:

          QRegularExpression regExp("^.{,32}$");
      

      In my same code this expression worked fine:

          QRegularExpression regExp("^[0-9]{0,8}$");
      
      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 25 May 2018, 18:26 last edited by raven-worx
      #2

      @Guerrian

      then why not try the following ;)

      QRegularExpression regExp("^[.]{0,32}$");

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • G Guerrian
        25 May 2018, 18:20

        This is what I tried to allow any string up to a maximum of 32 characters, but it does not work:

            QRegularExpression regExp("^.{,32}$");
        

        In my same code this expression worked fine:

            QRegularExpression regExp("^[0-9]{0,8}$");
        
        A Offline
        A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 25 May 2018, 18:30 last edited by
        #3

        @Guerrian

        It should already work if you change the pattern to

        "^.{0,32}$"

        Test: https://regex101.com/r/PN7hSo/1

        @raven-worx: I tried your pattern in the online tester but it did not match :(

        Qt has to stay free or it will die.

        R 1 Reply Last reply 25 May 2018, 18:37
        1
        • A aha_1980
          25 May 2018, 18:30

          @Guerrian

          It should already work if you change the pattern to

          "^.{0,32}$"

          Test: https://regex101.com/r/PN7hSo/1

          @raven-worx: I tried your pattern in the online tester but it did not match :(

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 25 May 2018, 18:37 last edited by
          #4

          @aha_1980
          odd, seems like "^[]" has some special meaning?
          It works when [] is replaced with () though

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          A 1 Reply Last reply 25 May 2018, 18:44
          0
          • R raven-worx
            25 May 2018, 18:37

            @aha_1980
            odd, seems like "^[]" has some special meaning?
            It works when [] is replaced with () though

            A Offline
            A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on 25 May 2018, 18:44 last edited by
            #5

            @raven-worx said in How to create a regular expression for any string up to a maximum length?:

            @aha_1980
            odd, seems like "^[]" has some special meaning?

            Don't ask me - in theory it should work :)

            It works when [] is replaced with () though

            Yeah, that creates a capture group around '.' and is needed if you want to re-use the result. Otherwise you can just use '.'

            Qt has to stay free or it will die.

            J 1 Reply Last reply 25 May 2018, 22:01
            0
            • A aha_1980
              25 May 2018, 18:44

              @raven-worx said in How to create a regular expression for any string up to a maximum length?:

              @aha_1980
              odd, seems like "^[]" has some special meaning?

              Don't ask me - in theory it should work :)

              It works when [] is replaced with () though

              Yeah, that creates a capture group around '.' and is needed if you want to re-use the result. Otherwise you can just use '.'

              J Offline
              J Offline
              JonB
              wrote on 25 May 2018, 22:01 last edited by
              #6

              I assume [.] would just be the same as writing \.

              1 Reply Last reply
              0

              4/6

              25 May 2018, 18:37

              • Login

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