Tiddlywiki uses internally a space-separated tags for making a list of tags. But it uses [[
and ]]
to limit multi-word tags.
That is, a list of foo
, ram doo
, bar
and very cool
becomes in tiddlywiki a string like that:
"foo [[ram doo]] bar [[very cool]]"
How can I transform that into python list that look like:
['foo', 'ram doo', 'bar', 'very cool']
"foo [[ram doo]] bar".split()
does not work for me..
With regex:
import re
a = "foo [[ram doo]] bar [[very cool]] something else"
pattern = re.compile(r'\[\[[^\]]+\]\]|[^\[\] ]+')
print([i.strip(' []') for i in pattern.findall(a)])
Prints ['foo', 'ram doo', 'bar', 'very cool', 'something', 'else']
Regex basically "tokenizes" the string (borders are either [[..]]
or space, in that order), the list comprehension then removes the brackets from the tokens.
A simple regular expression works:
>>> import re
>>> [x.strip() for x in re.split('\[\[|\]\]', "foo [[ram doo]] bar [[very cool]]") if x]
['foo', 'ram doo', 'bar', 'very cool']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With