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.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
kashif4u
  • 175
  • 1
  • 3
  • 13

1 Answers1

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://stackoverflow.com/a/3533526/2081889

Community
  • 1
  • 1
Taylor Hx
  • 2,815
  • 23
  • 36
  • 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 should always come with variable sales like or but editing pattern (?=.*of sales) will put 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 appearing on its own like \b(?=.*of)("missing variable")\b and with out variable (sales) can be detected. – kashif4u Apr 29 '14 at 16:37