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. removing characters from string to keep digits
QtWS25 Last Chance

removing characters from string to keep digits

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.7k 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.
  • A Offline
    A Offline
    Andse
    wrote on last edited by aha_1980
    #1

    vers ="VER 3.3a on ABC"
    #Then I try to remove all chars except ".0123456789"
    vers.remove(QRegularExpression("[-`~!@#$%^&*()_—+=|:;<>«»,?/{}'"\[\]\\]"));
    #hoping that I can do vers.toFloat() and get 3.3

    But vers is untouched.. Qt 5.11.3 / 5.12.6
    What is the correct way to extract the version number in this case?

    JonBJ 1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2
      QString result;
      for (const QChar c : qAsConst(vers)) {
        if (c.isDigit() || c == '.') {
          result.append(c);
        }
      }
      

      (Z(:^

      1 Reply Last reply
      5
      • A Offline
        A Offline
        Andse
        wrote on last edited by
        #3

        Thank you !

        1 Reply Last reply
        0
        • A Andse

          vers ="VER 3.3a on ABC"
          #Then I try to remove all chars except ".0123456789"
          vers.remove(QRegularExpression("[-`~!@#$%^&*()_—+=|:;<>«»,?/{}'"\[\]\\]"));
          #hoping that I can do vers.toFloat() and get 3.3

          But vers is untouched.. Qt 5.11.3 / 5.12.6
          What is the correct way to extract the version number in this case?

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

          @Andse
          Apart from doing it @sierdzio's way --- which is certainly much better than attempting to enumerate a whole bunch of characters to remove in order to be left with only digits --- in answer to your question, your regular expression looks malformed to me, where you try to specify " and ] characters. (In fact, copying what you have here gives an "unterminated expression", so not sure what exactly you have.) That part would need to be (untested):

          QRegularExpression("[...\"\\]]")
          

          Because of your ]\] ( <-- damn :( this comes out right in edit mode, but not after posting :( ), where it should be \\]], I suspect you have something like the character class enclosed by [...] followed by something to do with \\ or ], and that's why it does not match.

          1 Reply Last reply
          4

          • Login

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