Python regex distribution according to criteria

The next Python Regex question is to replace a line that is not located between two specific words since the answers were incomplete.

Given the string str, divide according to "::", ignoring the "::" that are between "<" and ">".

Expected inputs and outputs:

input a :: <<a :: b> c>::<a < a < b:: b> :: b> :: b> :: a output [a , <<a :: b> c>,<a < a < b:: b> :: b> :: b> , a] input a< b <c a>> output [a< b <c a>>] input a:<a b> output [a:<a b>] 
+1
python regex
source share
1 answer

Just an if else condition is needed for this case. This will lead to splitting if there is any substring :: present inside the input string, otherwise it will return the actual input string.

 >>> def csplit(s): if '::' in s: return [i for i in regex.split(r'(<(?:(?R)|[^<>])*>)|::', s) if i and i != ' '] else: return s >>> csplit('a :: <<a :: b> c>::<a < a < b:: b> :: b> :: b> :: a') ['a ', '<<a :: b> c>', '<a < a < b:: b> :: b> :: b>', ' a'] >>> csplit('a:<a b>') 'a:<a b>' >>> csplit('a< b <c a>>') 'a< b <c a>>' 
+1
source share

All Articles