I have the following string as an example:
string = "@@ cat $$ @@dog$^"
I want to extract all the stringa that are locked between "@@" and "$", so the output will be:
[" cat ","dog"]
I only know how to extract the first occurrence:
import re
r = re.compile('@@(.*?)$')
m = r.search(string)
if m:
result_str = m.group(1)
Thoughts & suggestions on how to catch them all are welcomed.
Use re.findall()
to get every occurrence of your substring. $
is considered a special character in regular expressions meaning — "the end of the string" anchor, so you need to escape $
to match a literal character.
>>> import re
>>> s = '@@ cat $$ @@dog$^'
>>> re.findall(r'@@(.*?)\$', s)
[' cat ', 'dog']
To remove the leading and trailing whitespace, you can simply match it outside of the capture group.
>>> re.findall(r'@@\s*(.*?)\s*\$', s)
['cat', 'dog']
Also, if the context has a possibility of spanning across newlines, you may consider using negation.
>>> re.findall(r'@@\s*([^$]*)\s*\$', s)
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