Given string like keydown.capture.once.prevent.shift.control[arrowdown,arrowup]:silent
I want to match shift.control[arrowdown,arrowup]:silent
And if the same keydown.capture.once.prevent.shift[arrowdown,arrowup]:silent
then it should match shift[arrowdown,arrowup]:silent
Here the keywords shift | control are optional, ex: keydown.capture.once.prevent.[arrowdown,arrowup]:silent
, matching string [arrowdown,arrowup]:silen
I wrote the below regex, it can only capture zero or one keyword, but the expected result is zero or all keywords matching into separate group
(shift|control)?\[(.*?)\]:silent
, how can we capture all the keywords if they exist?
Additional notes: Order of keywords doesn't matter, ex it can be control.shift[]:silent
CodePudding user response:
You may use any one of these regex solutions:
(?:(?:shift|control)\.?)*\[([^\]]*)\]:silent
This matches shift
or control` optionally followed by a dit and repeats this non-capture group 0 or more times.
(?:shift(?:\.control)?|control(?:\.shift)?)?\[([^\]]*)\]:silent
This one matches shift.control
OR control.shift
in an optional non-capture group with the parts after dot optional in each alternation.
CodePudding user response:
You could try this:
(?:(shift|control|shift\.control|control\.shift))?(\[.*\]):silent
Test and output: https://regex101.com/r/rMSd2l/1