Reputation: 3761
For the following string, is it possible for regex to return the comma delimited matches within the square brackets?
"root.path.definition[id=1234,test=blah,scope=A,B,C,D]"
Expected output would be:
["id=1234", "test=blah", "scope=A,B,C,D"]
The closest I have gotten so far is the following:
(?<=\[)(.*?)(?=\])
But this will only return one match for everything within the square brackets.
Upvotes: 1
Views: 116
Reputation: 163267
One option is to use the re module and first get the part between the square brackets using a capturing group and a negated character class.
\[([^][]*)]
That part will match:
\[
Match [
char([^][]*)
Capture group 1, match 0+ times any char other than [
and ]
]
A [
charThen get the separate parts by matching the key value pairs separated by a comma.
\w+=.*?(?=,\w+=|$)
That part will match:
\w+
Match 1+ word characters=
Match literally.*?(?=,\w+=|$)
Match as least as possible chars until you either encounter a comma, 1+ word characters and =
or the end of the stringFor example
import re
s = "root.path.definition[id=1234,test=blah,scope=A,B,C,D]"
m = re.search(r"\[([^][]*)]", s)
if m:
print(re.findall(r"\w+=.*?(?=,\w+=|$)", m.group(1)))
Output
['id=1234', 'test=blah', 'scope=A,B,C,D']
If you can make use of the regex module, this might also be an option matching the keys and values using lookarounds to assert the [
and ]
(?<=\[[^][]*)\w+=.*?(?=,\w+=|])(?=[^][]*])
For example
import regex
s = "root.path.definition[id=1234,test=blah,scope=A,B,C,D]"
print(regex.findall(r"(?<=\[[^][]*)\w+=.*?(?=,\w+=|])(?=[^][]*])", s))
Output
['id=1234', 'test=blah', 'scope=A,B,C,D']
Upvotes: 2