Results 1 to 5 of 5

Thread: QRegExp for removing all non alphanumeric charecters

  1. #1
    Join Date
    Apr 2009
    Posts
    17
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QRegExp for removing all non alphanumeric charecters

    Using QString I can remove any character from string, but what would be QRegExp to remove ALL non alphanumeric characters from that string. Currently I have written a function that will remove each character one by one, but I would like to know if i can do the same thing only in one step.

    Here is my function

    Qt Code:
    1. QString clean(QString str)
    2. {
    3. str.remove("\"");
    4. str.remove("\\");
    5. str.remove(".");
    6. str.remove(",");
    7. str.remove("?");
    8. str.remove("!");
    9. str.remove("'");
    10. str.remove("`");
    11. str.remove("-");
    12. str.remove("_");
    13. str.remove("—");
    14. str.remove("@");
    15. str.remove("#");
    16. str.remove("$");
    17. str.remove("%");
    18. str.remove("^");
    19. str.remove("&");
    20. str.remove("*");
    21. str.remove("(");
    22. str.remove(")");
    23. str.remove("{");
    24. str.remove("}");
    25. str.remove("[");
    26. str.remove("]");
    27. str.remove("|");
    28. str.remove(";");
    29. str.remove(":");
    30. str.remove("/");
    31. str.remove(">");
    32. str.remove("<");
    33. str.remove("~");
    34. str.remove("=");
    35. str.remove("+");
    36. str.remove(str.fromUtf8("«"));
    37. str.remove(str.fromUtf8("»"));
    38. str = str.trimmed();
    39. return str;
    40. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QRegExp for removing all non alphanumeric charecters

    Read the QRegExp class description in QtAssistant.
    I guess you want something similar to:
    Qt Code:
    1. str.remove(QRegExp("[^a-zA-Z\\d\\s]"));
    To copy to clipboard, switch view to plain text mode 
    but maybe there is a better way to write it
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  3. #3
    Join Date
    Apr 2009
    Posts
    17
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Re: QRegExp for removing all non alphanumeric charecters

    Thanks for suggestion.
    After some reading and trial&error here is the code that works

    Qt Code:
    1. str.remove(QRegExp(QString::fromUtf8("[-`~!@#$%^&*()_—+=|:;<>«»,.?/{}\'\"\\\[\\\]\\\\]")));
    To copy to clipboard, switch view to plain text mode 

    This will remove all none alphanumeric characters that i know.
    If anyone will use this code, save the file in UTF-8, otherwise it will not remove — «»

  4. #4
    Join Date
    Sep 2009
    Location
    Czech Republic
    Posts
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QRegExp for removing all non alphanumeric charecters

    Seems to be working (Calhal's solution would work only with ascii - no interpunction characters).

  5. #5
    Join Date
    Oct 2013
    Location
    Quebec
    Posts
    31
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QRegExp for removing all non alphanumeric charecters

    Hey, Sorry to revive an old thread.

    I am using your solution:

    Qt Code:
    1. QString Util::cleanQString(QString toClean) {
    2.  
    3. QString toReturn = toClean;
    4. toReturn.remove(QRegExp(QString::fromUtf8("[-`~!@#$%^&*()_—+=|:;<>«»,.?/{}\'\"\\\[\\\]\\\\]")));
    5. return toReturn;
    6. }
    To copy to clipboard, switch view to plain text mode 

    The compiler is giving me theses 4 warnings that I would like to fix:
    warning: C4129: '[' : unrecognized character escape sequence
    warning: C4129: ']' : unrecognized character escape sequence
    warning: C4129: '[' : unrecognized character escape sequence
    warning: C4129: ']' : unrecognized character escape sequence

    Using Qt 5.5.0 on VS2013

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.