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. QString - Removing non-alphanumeric chars
Forum Updated to NodeBB v4.3 + New Features

QString - Removing non-alphanumeric chars

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 6.4k Views 2 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.
  • M Offline
    M Offline
    maximus
    wrote on last edited by maximus
    #1

    I am using this function to remove non-alphanumeric from a QString. I use this function before saving to a file on the OS, I need this since theses char are not permitted by the OS for a filename.

    With this function (code under), The compiler is giving me warning that I would like to fix:
    warning: C4129: '[' : unrecognized character escape sequence
    warning: C4129: ']' : unrecognized character escape sequence

    Or maybe there is a function like this that already exists in QString or QFile?
    Thank you!

    QString Util::cleanQString(QString toClean) {  
        QString toReturn = toClean;
        toReturn.remove(QRegExp(QString::fromUtf8("[-`~!@#$%^&*()_—+=|:;<>«»,.?/{}\'\"\\\[\\\]\\\\]")));
        return toReturn;
    }
    

    Free Indoor Cycling Software - https://maximumtrainer.com

    Karen MorrisseyK 1 Reply Last reply
    0
    • M maximus

      I am using this function to remove non-alphanumeric from a QString. I use this function before saving to a file on the OS, I need this since theses char are not permitted by the OS for a filename.

      With this function (code under), The compiler is giving me warning that I would like to fix:
      warning: C4129: '[' : unrecognized character escape sequence
      warning: C4129: ']' : unrecognized character escape sequence

      Or maybe there is a function like this that already exists in QString or QFile?
      Thank you!

      QString Util::cleanQString(QString toClean) {  
          QString toReturn = toClean;
          toReturn.remove(QRegExp(QString::fromUtf8("[-`~!@#$%^&*()_—+=|:;<>«»,.?/{}\'\"\\\[\\\]\\\\]")));
          return toReturn;
      }
      
      Karen MorrisseyK Offline
      Karen MorrisseyK Offline
      Karen Morrissey
      wrote on last edited by Karen Morrissey
      #2

      @maximus You don't directly escape the "[" and "]", so "\\[", not "\\\[". The pattern string you want to use is "[-`~!@#$%^&*()_—+=|:;<>«»,.?/{}\'\"\\[\\]\\\\]".

      1 Reply Last reply
      2
      • M Offline
        M Offline
        maximus
        wrote on last edited by
        #3

        Thanks you, that fixed the error.
        QRegex are always like writting chinese to me


        Free Indoor Cycling Software - https://maximumtrainer.com

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          If I may, you should change for QRegularExpression (the regexp itself doesn't change). It has better performances.

          To help you build your regex, there's the regex example that you can use that provides a good starting point to write them.

          For QRegularExpression, you need to get the 5.6 sources to get the equivalent helper program.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

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