2

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

2 Answers 2

2

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']
Sign up to request clarification or add additional context in comments.

Comments

2

No need for a regex:

substrs = [x.strip() for x in lyne.split('|') if x]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.