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






is there a need to escape { always translated as literal unles used in a special character format

– Onyambu
Sep 10 '18 at 8:21







@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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ