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.
  • GuerrianG Offline
    GuerrianG Offline
    Guerrian
    wrote on 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

    raven-worxR aha_1980A 2 Replies Last reply
    1
    • GuerrianG Guerrian

      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}$");
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on 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
      • GuerrianG Guerrian

        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}$");
        
        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 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.

        raven-worxR 1 Reply Last reply
        1
        • aha_1980A aha_1980

          @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 :(

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on 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

          aha_1980A 1 Reply Last reply
          0
          • raven-worxR raven-worx

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

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on 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.

            JonBJ 1 Reply Last reply
            0
            • aha_1980A aha_1980

              @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 '.'

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

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

              1 Reply Last reply
              0

              • Login

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