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. QRegularExpression::wildcardToRegularExpression() not working?

QRegularExpression::wildcardToRegularExpression() not working?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.7k 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by Violet Giraffe
    #1

    I have a problem with wildcard expressions since migrating from QRegExp to QRegularExpression. QRegularExpression::wildcardToRegularExpression("*") returns this expression, is it correct?

    \A(?:[^/\\]*)\z

    I expect that this expression should match any string, since that's the meaning of the * wildcard/glob expression, but QRegularExpression(QRegularExpression::wildcardToRegularExpression("*")).match("abc").hasMatch() returns false. This is the same way I was matching wildcard expressions with QRegExp (except it didn't need the wildcardToRegularExpression() conversion). Is the expression broken, or is there a problem with my match code?

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

      QRegularExpression(QRegularExpression::wildcardToRegularExpression("*")).match("abc").hasMatch() returns true in my testing.
      What's your Qt version?

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Violet Giraffe
        wrote on last edited by Violet Giraffe
        #3

        You're right , good point, I oversimplified the example. That works for me as well. This doesn't work:

        const bool m = QRegularExpression(QRegularExpression::wildcardToRegularExpression("*")).match("E:/Development/Projects/").hasMatch();

        Qt 5.15.2, the latest available Qt 5. Windows, msvc_2019.

        JonBJ 1 Reply Last reply
        0
        • V Violet Giraffe

          You're right , good point, I oversimplified the example. That works for me as well. This doesn't work:

          const bool m = QRegularExpression(QRegularExpression::wildcardToRegularExpression("*")).match("E:/Development/Projects/").hasMatch();

          Qt 5.15.2, the latest available Qt 5. Windows, msvc_2019.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Violet-Giraffe
          Because the point of the regular expression is to fail against any / or \s. You should have shown what you were testing it against.

          any string, since that's the meaning of the * wildcard/glob expression

          Wildcard/globbing should not match directory separators.

          Discussed in detail at https://doc.qt.io/qt-5/qregularexpression.html#wildcardToRegularExpression, https://doc.qt.io/qt-5/qregularexpression.html#wildcard-matching

          V 1 Reply Last reply
          0
          • JonBJ JonB

            @Violet-Giraffe
            Because the point of the regular expression is to fail against any / or \s. You should have shown what you were testing it against.

            any string, since that's the meaning of the * wildcard/glob expression

            Wildcard/globbing should not match directory separators.

            Discussed in detail at https://doc.qt.io/qt-5/qregularexpression.html#wildcardToRegularExpression, https://doc.qt.io/qt-5/qregularexpression.html#wildcard-matching

            V Offline
            V Offline
            Violet Giraffe
            wrote on last edited by Violet Giraffe
            #5

            @JonB said in QRegularExpression::wildcardToRegularExpression() not working?:

            Because the point of the regular expression is to fail against any / or \s.

            That's absolutely stupid for a glob expression. Is there a way to fix this while still using wildcardToRegularExpression? Or is the only way to manually edit the returned expression, e. g. replace [^/\]* with .*?

            I have read most of the https://doc.qt.io/qt-5/qregularexpression.html page before asking here, nothing related to my problem is discussed there, let alone - in detail.

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

              Hi,

              Glob patterns work at the file name level not on full paths. As stated in the method documentation, the current implementation follows closely the definition listed there. You can go through the patch review of that method for more details.

              Out of curiosity, what is your goal using .* since you will match anything with that ?

              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
              1
              • V Offline
                V Offline
                Violet Giraffe
                wrote on last edited by
                #7

                Thank you for the clarification and explanation. I did see the remark that the implementation now follows those rules, and didn't check the rules themselves, my bad. It worked just as expected with QRegExp, so I hoped it would work with QRegularExpression the same way if I migrate the code correctly.

                Of course, the * glob is a corner case, usually it's a combination of text characters and wildcards. But it's important for me to support full paths. Now it's clear I need to change my surrounding code, once again - thank you for the explanation!

                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