I have a little trouble extracting these two lists ‘[‘item1’, ‘item2’, ‘item3’] [‘item4’, ‘item5′]’ here is an example of the code I have attempted
JavaScript
x
10
10
1
import re
2
3
pattern_matcher = re.compile(r"([,]])")
4
5
alien_string = "Offering ['item1', 'item2', 'item3'] (4331)[6785] Requesting ['item4', 'item5'] (6998)[6766]. {0.255457} (0 left in queue)..."
6
7
matches = pattern_matcher.fullmatch(alien_string)
8
9
print(matches)
10
The output I received was
JavaScript
1
2
1
Output: None
2
I would like to know how I can go about extracting these two lists from this long string specifically [‘item1’, ‘item2’, ‘item3’] and [‘item4’, ‘item5’]
Advertisement
Answer
JavaScript
1
11
11
1
import re
2
3
pattern = re.compile(r"[[^]]+?(?:, [^]]+?)+]")
4
5
alien_string = "Offering ['item1', 'item2', 'item3'] (4331)[6785] Requesting ['item4', 'item5'] (6998)[6766]. {0.255457} (0 left in queue)..."
6
7
matches = re.findall(pattern, alien_string)
8
9
for match in matches:
10
print(match)
11
Result:
JavaScript
1
3
1
['item1', 'item2', 'item3']
2
['item4', 'item5']
3
Explanation:
[
matches the first bracket[^]]
matches anything that isnt a closed bracket[^]]*?
matches a variable number of non-bracket chars until the comma(?:
starts a non-capturing group (used forfindall
)
The rest should be self-explanatory.