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
QtWS25 Last Chance

QRegExp doesn't see \n character

Scheduled Pinned Locked Moved General and Desktop
qregexpqplaintextedit
2 Posts 2 Posters 1.0k Views
  • 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 23 Sept 2015, 21:59 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
    • J Offline
      J Offline
      JohanSolo
      wrote on 24 Sept 2015, 05:38 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

      1/2

      23 Sept 2015, 21:59

      • Login

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