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
Forum Updated to NodeBB v4.3 + New Features

removing characters from string to keep digits

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.8k Views 1 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.
  • A Offline
    A Offline
    Andse
    wrote on 3 Apr 2020, 06:51 last edited by aha_1980 4 Apr 2020, 06:59
    #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?

    J 1 Reply Last reply 3 Apr 2020, 07:45
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 3 Apr 2020, 07:09 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 3 Apr 2020, 07:30 last edited by
        #3

        Thank you !

        1 Reply Last reply
        0
        • A Andse
          3 Apr 2020, 06:51

          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?

          J Offline
          J Offline
          JonB
          wrote on 3 Apr 2020, 07:45 last edited by JonB 4 Mar 2020, 07:47
          #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

          1/4

          3 Apr 2020, 06:51

          • 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