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 doesn't see \n character
Forum Update on Tuesday, May 27th 2025

QRegExp doesn't see \n character

Scheduled Pinned Locked Moved General and Desktop
qregexpqplaintextedit
2 Posts 2 Posters 1.0k 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.
  • Y Offline
    Y Offline
    yutkin
    wrote on last edited by yutkin
    #1

    I'm trying to handle QPlainTextEdit input:

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

    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?

    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by
      #2

      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]+".

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      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