Wednesday, August 15, 2018

.net - Let a regular expression ignore the order of the search terms










I let my users enter a search term: "foo bar".



I would to convert the user query to a string that matches the words, but ignoring the order:




bla foo bla bar   = match
^^^ ^^^
bla bar bla foo = match (order = reversed)
^^^ ^^^
bla bla bla foo = no match (only one word)
^^^
bar bla bla bla = no match (only one word)
^^^



Note: The example contains 2 words, but my users will go wild, and will enter more words.



If the order is not to be ignored, it is simple: replace a spaces by ".*" and I have a very good match.



But how can I let the regular expression ignore the order, and still match both (or more) words?



Do I need to generate a regex that will match possible combinations of order?


Answer



See this answer: Regular Expressions: Is there an AND operator?



No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...