Regex Python with order for OR | pattern -
how can return ordered list regex? still kind of learning regex.
for example, lets have list = [a,b,c,c,b,a,g] , want have b's first, followed a's, lastly a's in list. how can regex it?
i thinking: pattern = re.compile('b|c|a') [letter letter in list if pattern.match(letter)]
but comes out ['a','b','c','c','b','a']
what want ['b','b','c','c','a','a']
how possible? thanks!
regular expressions dealing patterns in text, search , extract substrings string. want actual intelligent processing. outside scope of regex put quite easy in python :)
you make sound there should 'a', 'b', or 'c' in list can write simple comparison function
def cmp(c): return {'a' : 1, 'b' : 0, 'c' : 2}[c]
then give sorted
sorted(your_list, key=cmp)
simple that.
Comments
Post a Comment