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 Update on Monday, May 27th 2025

matching multiple patterns wildcard and regexp

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.7k 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.
  • U Offline
    U Offline
    user4592357
    wrote on 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?

    JonBJ 1 Reply Last reply
    0
    • U user4592357

      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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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
      0
      • JonBJ JonB

        @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 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
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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
          2
          • SGaistS SGaist

            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 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
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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
              1
              • SGaistS SGaist

                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 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).

                JonBJ 1 Reply Last reply
                0
                • U user4592357

                  @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).

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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

                  • Login

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