What is the RE to match the list?
Clash Royale CLAN TAG#URR8PPP
What is the RE to match the list?
I want to know how to construct the regular express to extract the list.
Here is my string:
audit = "## audit_filter = ['hostname.*','service.*'] ##"
Here is my expression:
AUDIT_FILTER_RE = r'([.*])'
And here is my search statement:
audit_filter = re.search(AUDIT_FILTER_RE, audit).group(1)
I want to extract everything inside the square brackets including the brackets. '[...]'
Expected Output:
['hostname.*','service.*']
print(re.findall(r"[(.*?)]", audit))
I might recommend
([[^]]+])
, in case of multiple lists (though not nested lists.)– Patrick Haugh
Aug 7 at 19:01
([[^]]+])
2 Answers
2
import re
audit = "## audit_filter = ['hostname.*','service.*'] ##"
print eval(re.findall(r"[.*]", audit)[0]) # ['hostname.*', 'service.*']
findall
returns a list of string matches. In your case, there should only be one, so I'm retrieving the string at index 0, which is a string representation of a list. Then, I use eval(...)
to convert that string representation of a list to an actual list. Just beware:
findall
eval(...)
eval()
Thank you Sir! This is exactly what I needed!
– python_newbie
Aug 7 at 20:27
Great. Since this solved the problem, could you please mark it as the accepted answer?
– Emil
Aug 7 at 20:46
Use r"[(.*?)]"
r"[(.*?)]"
Ex:
import re
audit = "## audit_filter = ['hostname.*'] ##"
print(re.findall(r"[(.*?)]", audit))
Output:
["'hostname.*'"]
The RE picks up the double quotes? I just want the single quotes and no double quotes...
– python_newbie
Aug 7 at 19:05
No....That is actually a string
"RESULT"
– Rakesh
Aug 7 at 19:07
"RESULT"
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.
print(re.findall(r"[(.*?)]", audit))
– Rakesh
Aug 7 at 18:59