match string in any word order regex
Asked Answered
F

1

5

I am having to write few patterns which will be used for matching purpose through Regex upon user input:

string pattern = "^.*overview\ of\ sales.*customer\ information.*$";
string input = "overview of sales with customer information";

Is there a way(s) to remove the word order in Regex? So

string pattern = "^.*overview\ of\ sales.*customer\ information.*$";

will also match:

string input = "customer information with overview of sales";

This can probably be done by writing every pattern in reverse order but as number of patterns are quite few and will grow with time and number of *. This tends to be tedious way of doing it so please guide on this matter.

Flexor answered 10/11, 2013 at 22:55 Comment(0)
C
14

You'll want to use positive lookahead ?=.*.

[Positive lookahead]q(?=u)matches a q that is followed by a u, without making the u part of the match. The positive lookahead construct is a pair of parentheses, with the opening parenthesis followed by a question mark and an equals sign.

Positive lookahead can be used in this circumstance to match a set of patterns in any order, like so:

(?=.*customer)(?=.*information)(?=.*with)(?=.*overview)(?=.*of)(?=.*sales)

Regular expression visualization

Debuggex Demo Here

To modify, just add, edit or remove words as you see fit.

EDIT

Found a question that explains the same concept here: https://mcmap.net/q/274757/-regex-i-want-this-and-that-and-that-in-any-order

Cacciatore answered 11/11, 2013 at 0:7 Comment(1)
Hi Daemon, can I trouble you by asking on how to edit above pattern to find one word on specific position in user utterance. i.e. from above pattern, word <of> should always come with variable sales like <of sales> or <sales of> but editing pattern (?=.*of sales) will put <of> inside the block, hence will only work with sales. What I am looking for is to set condition similar to \b(?=.*of)(?=.*sales)\b so <of> appearing on its own like \b(?=.*of)("missing variable")\b and with out variable (sales) can be detected.Flexor

© 2022 - 2024 — McMap. All rights reserved.