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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With