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. Help with making regular expression

Help with making regular expression

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.1k 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.
  • B Offline
    B Offline
    bagipro
    wrote on last edited by
    #1

    @QString str("AZ ZA 1234 XX");
    QRegExp reg("[A-Z0-9]{2}");
    int pos = reg.indexIn(str);
    while(pos >= 0)
    {
    qDebug() << reg.cap(0);
    pos = reg.indexIn(str, pos+1);
    }@

    I'm going to get "AZ", "ZA", "XX", but get "AZ", "ZA", "12", "23", "34", "XX"
    How to fix?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bagipro
      wrote on last edited by
      #2

      @QRegExp reg("(?:^|\s)([A-Z0-9]{2})(?:\s|$)");@
      But I'm getting spaces too, but I don't want

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        You are including digits, but you don't want them, right? Then just remove that part.
        @
        QRegExp reg("[A-Z]{2}");
        @

        (Z(:^

        1 Reply Last reply
        0
        • B Offline
          B Offline
          bagipro
          wrote on last edited by
          #4

          No, I want to get letter+letter or letter+number or number+number.
          So, if I input "12 1234 AA 0 A1 1A ZZ", I want to get "12", "AA", "A1", "1A", "ZZ"

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            Ah, ok. You need to either add space to your regexp, or notify it somehow that you want to parse only whole words (check out the documentation for exact syntax).

            Or, you can split your string using space as separator, and then feed all substrings into the regexp.

            (Z(:^

            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