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. matching multiple patterns wildcard and regexp
Forum Updated to NodeBB v4.3 + New Features

matching multiple patterns wildcard and regexp

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 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.
  • U Offline
    U Offline
    user4592357
    wrote on 13 Apr 2022, 17:35 last edited by
    #1

    the line edit input in my application allows to enter string to search among items by matching the string by wildcard or regexp.

    i need to support input of multiple patterns such as abc* *def and filter items based on any of them. i tried to split the input by space, then concatenate them by | to allow matching with OR operator. this would allow to iterate over the items once.
    so i have (abc*|*def) but when i set this pattern to QRegExp class, it doesn't correctly match the items.

    so current solution for me is to split the input by space, then for each pattern iterate over all items and filter items based on each pattern. this means iterating over the set as many times as the patterns.

    what can i do to optimize the solution?

    J 1 Reply Last reply 13 Apr 2022, 17:40
    0
    • U user4592357
      13 Apr 2022, 17:35

      the line edit input in my application allows to enter string to search among items by matching the string by wildcard or regexp.

      i need to support input of multiple patterns such as abc* *def and filter items based on any of them. i tried to split the input by space, then concatenate them by | to allow matching with OR operator. this would allow to iterate over the items once.
      so i have (abc*|*def) but when i set this pattern to QRegExp class, it doesn't correctly match the items.

      so current solution for me is to split the input by space, then for each pattern iterate over all items and filter items based on each pattern. this means iterating over the set as many times as the patterns.

      what can i do to optimize the solution?

      J Offline
      J Offline
      JonB
      wrote on 13 Apr 2022, 17:40 last edited by
      #2

      @user4592357 said in matching multiple patterns wildcard and regexp:

      so i have (abc*|*def) but when i set this pattern to QRegExp class, it doesn't correctly match the items.

      Should you be using QRegularExpression? What version of Qt?

      U 1 Reply Last reply 13 Apr 2022, 17:48
      0
      • J JonB
        13 Apr 2022, 17:40

        @user4592357 said in matching multiple patterns wildcard and regexp:

        so i have (abc*|*def) but when i set this pattern to QRegExp class, it doesn't correctly match the items.

        Should you be using QRegularExpression? What version of Qt?

        U Offline
        U Offline
        user4592357
        wrote on 13 Apr 2022, 17:48 last edited by
        #3

        @JonB will QRegularExpression fix that? i think i also need to add code to make it work for wildcard.
        it's qt 5.15.x

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 13 Apr 2022, 18:19 last edited by
          #4

          Hi,

          QRegExp is deprecated in Qt 5 and removed in Qt 6 so go with QRegularExpression.

          To build proper wildcard regular expression, take a look at QRegularExpression::wildcardToRegularExpression.

          That said, can you show a real input example and group of expression you want to use ? And what result you would expect to have ?

          On a side note, there's a helper tool you can build and run to test your regular expressions.

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

          U 1 Reply Last reply 13 Apr 2022, 18:30
          2
          • S SGaist
            13 Apr 2022, 18:19

            Hi,

            QRegExp is deprecated in Qt 5 and removed in Qt 6 so go with QRegularExpression.

            To build proper wildcard regular expression, take a look at QRegularExpression::wildcardToRegularExpression.

            That said, can you show a real input example and group of expression you want to use ? And what result you would expect to have ?

            On a side note, there's a helper tool you can build and run to test your regular expressions.

            U Offline
            U Offline
            user4592357
            wrote on 13 Apr 2022, 18:30 last edited by
            #5

            @SGaist ok i will replace it with QRegularExpression.

            this is an example input and expected result:
            specified pattern: "abc* def*" (wildcard)
            inputs: rbdtgh abcert defhij jikdef ftgabc kilgws
            output: abcert defhij

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 13 Apr 2022, 18:38 last edited by
              #6

              I don't know your knowledge level on the regexp subject but the output you expect won't be returned by the expression you suggest.

              At best you would have everything including and after abcert since your expression more or less says: abc followed by anything or nothing, followed by a space followed by def followed by anything or nothing.

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

              U 1 Reply Last reply 13 Apr 2022, 18:43
              1
              • S SGaist
                13 Apr 2022, 18:38

                I don't know your knowledge level on the regexp subject but the output you expect won't be returned by the expression you suggest.

                At best you would have everything including and after abcert since your expression more or less says: abc followed by anything or nothing, followed by a space followed by def followed by anything or nothing.

                U Offline
                U Offline
                user4592357
                wrote on 13 Apr 2022, 18:43 last edited by user4592357
                #7

                @SGaist i would have to have a different logic that the normal one.
                i have to split this input pattern string by space to retrieve separate patterns (space splitting is safe since my string set can't contain space). then i need to match each of my strings to each pattern. this has complexity O(n*m), where n is the number of strings in set, and m patterns. so i was wondering if it can be made O(n).

                J 1 Reply Last reply 13 Apr 2022, 18:51
                0
                • U user4592357
                  13 Apr 2022, 18:43

                  @SGaist i would have to have a different logic that the normal one.
                  i have to split this input pattern string by space to retrieve separate patterns (space splitting is safe since my string set can't contain space). then i need to match each of my strings to each pattern. this has complexity O(n*m), where n is the number of strings in set, and m patterns. so i was wondering if it can be made O(n).

                  J Offline
                  J Offline
                  JonB
                  wrote on 13 Apr 2022, 18:51 last edited by JonB
                  #8

                  @user4592357 said in matching multiple patterns wildcard and regexp:

                  this has complexity O(n*m), n string set, m patterns. so i was wondering if it can be made O(n).

                  :)

                  BTW, do you have hundreds of patterns to match against hundred of "words"?

                  A regular expression like

                  \b(abc|def|ghi)\B*
                  

                  might/should deliver multiple matches against your patterns. Of course you would have to build that pattern string from your desired strings. Whether it would be faster than doing your own stuff on each word in practice I couldn't say. You might think of it as O(n), but there's quite a bit going on inside the regular expression matching code to deal with the m patterns from the O(n * m) that you are passing in via the (...|...) in the string. No such thing as a free lunch ;-)

                  1 Reply Last reply
                  1

                  1/8

                  13 Apr 2022, 17:35

                  • Login

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