Let’s say a string can be made up of the characters a, b, or c.
Is it possible through a regular expression to group the characters of a string by rules – with the most strict rules being applied first, followed by weaker rules?
For example:
- Priority rule: identify “ba” appearing together in a string.
- All remaining characters will be their own groups.
Therefore, a string such as abbadaabad would group to:
a, b, ba, d, a, a, ba, d
My thinking is that once I can set up a two-level rule set, I could create a deeper ruleset with more refined rules.
In an alternation rule, the first matching expression wins.
This pattern will match
baif it is found, otherwise it will match individual characters:Here an example usage in Python: