Suppose I have a string like str = "[Hi all], [this is] [an example] "
. I want to split it into several pieces, each of which consists content inside a pair bracket. In another word, i want to grab the phrases inside each pair of bracket. The result should be like:
['Hi all', 'this is', 'an example']
How can I achieve this goal using a regular expression in Python?
data = "[Hi all], [this is] [an example] "
import re
print re.findall("\[(.*?)\]", data) # ['Hi all', 'this is', 'an example']
Debuggex Demo
Try this:
import re
str = "[Hi all], [this is] [an example] "
contents = re.findall('\[(.*?)\]', str)
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