Parse string with words surrounded by curly braces to array in Python 3.5
Parse string with words surrounded by curly braces to array in Python 3.5
I've a string like this:
"asdf FIELD1n adf FIELD2 asdf adsfFIELD3asdf FIELD4"
What I need is an array containing the strings "FIELD1", "FIELD2", "FIELD3", "FIELD4". In other words: Find all text enclosed by and and put it to an array. Is there an easy/clever way to do this with Python 3.5 ?
2 Answers
2
easy with regular expressions as long as the braces cannot be nested:
>>> import re
>>> re.findall(r"(.*?)","asdf FIELD1n adf FIELD2 asdf adsfFIELD3asdf FIELD4")
['FIELD1', 'FIELD2', 'FIELD3', 'FIELD4']
findall conveniently creates a list of all matching expressions in the input.
findall
list
the (.*?) expression extracts the data between the curly braces (that you have to escape because they are special in regex language (repeating groups), even if they aren't interpreted as such in this particular context, so they could be omitted).
(.*?)
The parentheses are used to only extract the non-curly brace part and the .*? makes sure that you're matching the closest closing curly brace (non-greedy mode).
.*?
It's also good practice to use raw string prefix for regexes (even if it's not absolutely useful here, it avoids the infamous 1 and b traps)
1
b
@Onyambu technically no... they're only special after a previous expression... but there's no harm :) (if you suddenly through in a pattern to match previously, then things will go odd without the escaping though)
– Jon Clements♦
Sep 10 '18 at 8:22
@Onyambu you're right. They aren't absolutely necessary in that particular case.
– Jean-François Fabre
Sep 10 '18 at 8:22
@Onyambu but why? :p
– Jon Clements♦
Sep 10 '18 at 8:26
and there was me thinking I needed help :p
– Jon Clements♦
Sep 10 '18 at 8:39
For completeness sake you can also use str.split to obtain your desired output:
str.split
s = "asdf FIELD1n adf FIELD2 asdf adsfFIELD3asdf FIELD4"
[i.split('')[0] for i in s.split('{')[1:]]
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
is there a need to escape
{always translated as literal unles used in a special character format– Onyambu
Sep 10 '18 at 8:21