Qt World Summit: Submit your Presentation

QRegExp doesn't see \n character

  • I'm trying to handle QPlainTextEdit input:

    auto text = ui->textEdit->toPlainText().toLower();
    auto regexp = QRegExp("[^0-9\\s\\n]");
    text.replace(regexp, "");

    It should accepts only digits, space character and transition to new line (\n). It works for digits and space character but when I'm trying to press enter for new line - nothing happens. How can I fix it?

  • What OS and Qt version are you using?
    On windows the end-of-line (EOL) marking is done by \r\n, although I'm not sure how it is done in the QPlainTextEdit class, but it may worth to give a try.

    IIRC QRegExp should be replaced QRegularExpression in recent code, but I don't think this is the problem.
    And I'm not sure about the regex you posted: the caret should negate the content of the class, therefore matching any character not being a number or a spacing, resulting in exactly the opposite.
    If you basically basically want to reject any string containing a letter, then you could try to reverse your regex to "[^a-z,A-z]+".

Log in to reply