First of all, the question is tagged with Python and regex tags, but it is not tied to them - the answer may be high.
At the moment, I am separating a multiple-delimited string with the following pattern. Actually there are more differentiating patterns, and they are more complex, but let this simplify and limit them to two characters - # and * :
parts = re.split('#|*', string)
What is this approach? aaa#bbb*ccc#ddd string is divided into 4 substrings aaa , bbb , ccc , ddd . But separation is required either by a separator that occurs first in the string, or by a separator that most commonly occurs in the string. aaa#bbb*ccc#ddd should be divided into aaa , bbb*ccc , ddd and aaa*bbb#ccc*ddd should be divided into aaa , bbb#ccc , ddd .
I know a simple way to achieve this is to find which separator occurs first or is the most frequent in the string, and then split with that single separator. But the method needs to be effective, and I wonder if this can be achieved with a single regex expression. The main question is to split with the first occurrence of many separators - for the most common case, the delimiter will almost certainly need to calculate the number of errors in advance.
Update:
The question does not require separation by the first occurrence or the most frequent separator at the same time - any of these methods individually will be enough. I understand that splitting with the most common delimiter is not possible with a regular expression without first defining the delimiter, but I think that there is a possibility that splitting in the first case is possible with a regular expression and look without preliminary preparation.
source share