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. QRegExp issue
Qt 6.11 is out! See what's new in the release blog

QRegExp issue

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 830 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.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #1

    Hi! I want to remove special characters from QLineEdit text.

    Code:
    qDebug() << htmlCharsCheck(ui->lineEdit->text());

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

    The problem is that it doesn't remove those characters from the QString. For example it returns <b>[b][/b].
    How to fix it? Thanks.

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @Cobra91151,

      Side note: QRegularExpression supersedes QRegExp - see http://doc.qt.io/qt-5/qregularexpression.html#notes-for-qregexp-users for example.

      Anyway, there's a few problems with the pattern: the escapes are not right, for example ' doesn't need escaping, nor does the final ], and the second-last \] should be \\]. The pattern should be:

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

      Also, depending on your needs, you might be better off with something like this instead:

          testStr.remove(QRegExp(QString::fromUtf8("[^a-zA-Z \\s]")));
      

      Or perhaps even:

          return testStr.toHtmlEscaped();
      

      Which does give quite a different result for your sample input, but might be more in line with what you want to achieve?

      Cheers.

      Cobra91151C 1 Reply Last reply
      4
      • Paul ColbyP Paul Colby

        Hi @Cobra91151,

        Side note: QRegularExpression supersedes QRegExp - see http://doc.qt.io/qt-5/qregularexpression.html#notes-for-qregexp-users for example.

        Anyway, there's a few problems with the pattern: the escapes are not right, for example ' doesn't need escaping, nor does the final ], and the second-last \] should be \\]. The pattern should be:

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

        Also, depending on your needs, you might be better off with something like this instead:

            testStr.remove(QRegExp(QString::fromUtf8("[^a-zA-Z \\s]")));
        

        Or perhaps even:

            return testStr.toHtmlEscaped();
        

        Which does give quite a different result for your sample input, but might be more in line with what you want to achieve?

        Cheers.

        Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by
        #3

        @Paul-Colby

        Yes, it's working well. Thank you.

        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