'Regular expression to match words in any order but words can be optional
I'm looking around for a quite time and couldn't get a regular expression which fulfills my requirement
I have multiple lines of text something like this:
male positive average
average negative female
good negative female
female bad
male average
male
female
...
...
In above example there are three groups of words (male|female), (good|average|bad), and (positive|negative)
I want to capture each group of words in a named group: gender, quality, and feedback respectively.
The closest I reached is:
(?=.*(?P<gender>\b(fe)?male\b))(?=.*(?P<quality>(green|amber|red)))(?=.*(?P<feedback>(positive|negative))).*
Which matches the groups: gender, quality, and feedback in any order.
But it doesn't match or/and create a named group for the following type of sentences:
female green
positive male
positive female
female bad
male average
male
female
Note: The gender (male|female) is common and will be there in every line. Also, just for simplicity, here it's been mentioned just three different groups. Based on requirements it can even grow more.
Any help will be greatly appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
