Can you create a Python list from a string, while keeping characters in specific keywords together? -


i want create list characters in string, keep specific keywords together.

for example:

keywords: car, bus

input:

"xyzcarbusabccar" 

output:

["x", "y", "z", "car", "bus", "a", "b", "c", "car"] 

with re.findall. alternate between keywords first.

>>> import re >>> s = "xyzcarbusabccar" >>> re.findall('car|bus|[a-z]', s) ['x', 'y', 'z', 'car', 'bus', 'a', 'b', 'c', 'car'] 

in case have overlapping keywords, note solution find first 1 encounter:

>>> s = 'abcaratab' >>> re.findall('car|rat|[a-z]', s) ['a', 'b', 'car', 'a', 't', 'a', 'b'] 

you can make solution more general substituting [a-z] part whatever like, \w example, or simple . match character.

short explanation why works , why regex '[a-z]|car|bus' not work: regular expression engine tries alternating options left right , "eager" return match. means considers whole alternation match 1 of options has been matched. @ point, not try of remaining options stop processing , report match immediately. '[a-z]|car|bus', engine report match when sees character in character class [a-z] , never go on check if 'car' or 'bus' matched.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -