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. [Solved] QRegExp for a repeated pattern?

[Solved] QRegExp for a repeated pattern?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 988 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.
  • A Offline
    A Offline
    Ailurus
    wrote on last edited by Ailurus
    #1

    Does QRegExp support matching of repeated patterns? Let's consider a sequence of integers (some of which could be negative), separated by either a comma or a space. For example, 4,6,-28,10.

    The QRegExp for an optional minus, 1+ digits and a separator (comma or space) would be

    QRegExp pattern("-?[0-9]+[,\\s]");
    

    So I'd expect

    QRegExp repeatedPattern("[-?[0-9]+[,\\s]]*");
    

    to be the version for the repeated pattern described above. I know nested regular expressions can be ambiguous, but in this case it is not.

    To provide some context — I'm trying to constrain the input for a QLineEdit, using

    ui->lineEdit->setValidator(new QRegExpValidator(pattern));
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Unless you're using Qt 4, you should change to QRegularExpression then:

      ui->lineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("(-?\\d+[,\\s])+")));
      

      should do what you want.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        Unless you're using Qt 4, you should change to QRegularExpression then:

        ui->lineEdit->setValidator(new QRegularExpressionValidator(QRegularExpression("(-?\\d+[,\\s])+")));
        

        should do what you want.

        Hope it helps

        A Offline
        A Offline
        Ailurus
        wrote on last edited by
        #3

        Excellent! Thanks.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You're welcome !

          By the way, you don't have to edit your thread title anymore to mark the thread as solved, juts use the "Topic Tool" button.

          Also, while browsing the forum, please consider up-voting answers that helped you. It will make them easier to find for other forum users :)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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