Split text into a list on occurrences of the given separation character `sep_char`. The separation character may be escaped by a backslash to avoid splitting at that location. The separation character must be a string of size 1. If `maxsplit` is given, at most `maxsplit` splits are
(text, sep_char, maxsplit=-1)
| 24 | |
| 25 | |
| 26 | def escaped_split(text, sep_char, maxsplit=-1): |
| 27 | """Split text into a list on occurrences of the given separation |
| 28 | character `sep_char`. The separation character may be escaped by a |
| 29 | backslash to avoid splitting at that location. |
| 30 | |
| 31 | The separation character must be a string of size 1. |
| 32 | |
| 33 | If `maxsplit` is given, at most `maxsplit` splits are done (thus, |
| 34 | the list will have at most `maxsplit + 1` elements). If `maxsplit` |
| 35 | is not specified or less than 0, then there is no limit on the |
| 36 | number of splits (all possible splits are made). |
| 37 | """ |
| 38 | assert ( |
| 39 | len(sep_char) == 1 |
| 40 | ), "separation string must be a single character for escaped splitting" |
| 41 | |
| 42 | if maxsplit == 0: |
| 43 | return text |
| 44 | maxsplit = max(0, maxsplit) |
| 45 | |
| 46 | return re.split(r"(?<!\\)" + sep_char, text, maxsplit) |
| 47 | |
| 48 | |
| 49 | def simple_parse_args_string(args_string): |