I am new to regex in python, while the simple cases are a breeze, I am having troubles extracting multiple words from a string using. The string looks like this:
lyne = "| 0x008d | 2345| 0xe54b5b42 | 0520 | 0x02 GREEN| 4 RED |"
and access the different substrings between the | | using match.group
is there a way to do this...can anyone please help
Thanks
无需正则表达式:
substrs = [x.strip() for x in lyne.split('|') if x]
Any reason you can't just use lyne.split('|')
which'll do it, otherwise use re.split()
with the same thing...
If you really want a regexp (to match/find instead of split), then (stripping out spaces):
>>> re.findall(r'\|?\s*(.*?)\s*\|', lyne)
['0x008d', '2345', '0xe54b5b42', '0520', '0x02 GREEN', '4 RED']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.