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. Strange behavior of QString::indexOf (with QRegExp)
QtWS25 Last Chance

Strange behavior of QString::indexOf (with QRegExp)

Scheduled Pinned Locked Moved Solved General and Desktop
indexofqstringqregexp
10 Posts 2 Posters 5.5k 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
    Asperamanca
    wrote on 12 Nov 2015, 11:44 last edited by A Former User
    #1

    I have the following test string:
    Abcdefgh/Ijklmn - Opqrstuv__wxyz
    I have the following RegExp:

    QRegExp regExp;
    regExp.setPatternSyntax(QRegExp::RegExp2);
    regExp.setPattern("[_-./ ]");
    

    Then I do the following call to indexOf:

    int matchPosition = myString.indexOf(regExp, 0);

    ...and receive 0, where I expect 9.

    Any ideas where I might have gone wrong?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      TheBadger
      wrote on 12 Nov 2015, 11:50 last edited by
      #2

      I think you need to escape the "[_-./ ]" as follow:

      regExp.setPattern("[_\\-./ ]");
      

      Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on 12 Nov 2015, 12:06 last edited by
        #3

        Seems like QRegExp::escape should help here. Thanks for the pointer!

        A 1 Reply Last reply 12 Nov 2015, 12:11
        0
        • A Asperamanca
          12 Nov 2015, 12:06

          Seems like QRegExp::escape should help here. Thanks for the pointer!

          A Offline
          A Offline
          Asperamanca
          wrote on 12 Nov 2015, 12:11 last edited by Asperamanca 11 Dec 2015, 12:20
          #4

          Ok, so QRegExp::escape changes my pattern to
          [_-\./ ]

          Position is still zero.
          BTW. why would you put two backslashes in front of the hyphen?

          EDIT: It works after all, recompile helped.
          EDIT2: Correction: It only works when starting the indexOf at position 1, not at position 0. What's so special about the first character ('A')?

          T 1 Reply Last reply 12 Nov 2015, 12:23
          0
          • A Asperamanca
            12 Nov 2015, 12:11

            Ok, so QRegExp::escape changes my pattern to
            [_-\./ ]

            Position is still zero.
            BTW. why would you put two backslashes in front of the hyphen?

            EDIT: It works after all, recompile helped.
            EDIT2: Correction: It only works when starting the indexOf at position 1, not at position 0. What's so special about the first character ('A')?

            T Offline
            T Offline
            TheBadger
            wrote on 12 Nov 2015, 12:23 last edited by
            #5

            @Asperamanca said:

            QRegExp::escape changes my pattern to [_-\./ ]

            So it seems like my guess that it was an escape issue was correct, just the char was wrong :)

            why would you put two backslashes in front of the hyphen?

            So you need to escape the special characters, so in effect you want \., so if you pass that as a QString you end up with escaping the . for the string, not the regular expression, so you need two so that \\.gets seen by the QString as \. (So you escape the slash because the string should get a slash char).

            I hope you understand the explanation, its a bit difficult to explain.


            Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

            1 Reply Last reply
            0
            • T Offline
              T Offline
              TheBadger
              wrote on 12 Nov 2015, 12:27 last edited by
              #6

              What version of Qt are you using?

              For Qt 5, rather start using QRegularExpression instead of QRegExp (motivation)

              What's so special about the first character

              That is strange


              Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Asperamanca
                wrote on 12 Nov 2015, 12:27 last edited by Asperamanca 11 Dec 2015, 12:28
                #7

                To make sure, I removed the dot from my regexp pattern. All remaining characters shouldn't need escaping.
                Problem persists, so it doesn't seem to be an escaping issue.

                Edit: Qt 4.8.1

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  TheBadger
                  wrote on 12 Nov 2015, 12:29 last edited by TheBadger 11 Dec 2015, 12:31
                  #8

                  It is useful to test your regular expressions on a website that does matching, I use the following one: http://regexr.com/

                  On that website the issue is that the - needed to be escaped...

                  Edit: That website handles more complex matching than what QRegExp support, but for basic matching it is useful to test like that (then remember proper escaping)


                  Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Asperamanca
                    wrote on 12 Nov 2015, 12:35 last edited by Asperamanca 11 Dec 2015, 12:38
                    #9

                    Interesting. Here is a list of characters needing to be escaped, according to my QRegExp sources:

                            case '$':
                            case '(':
                            case ')':
                            case '*':
                            case '+':
                            case '.':
                            case '?':
                            case '[':
                            case '\\':
                            case ']':
                            case '^':
                            case '{':
                            case '|':
                            case '}':
                    

                    No hyphen...but I can remove that as well, for the test.

                    EDIT: And here we have the explanation:
                    Qt bug

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      TheBadger
                      wrote on 12 Nov 2015, 12:41 last edited by TheBadger 11 Dec 2015, 12:41
                      #10

                      Reading that report and thinking about it it makes sense that it is not an actual bug.

                      For [a-z] it should not be escaped, since you want to match all characters from a up to character z.

                      For [a\-z] you want to match character a, or character hyphen or character z.


                      Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

                      1 Reply Last reply
                      0

                      10/10

                      12 Nov 2015, 12:41

                      • Login

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