My string is:
s = '"location":{"state":"WA"},"active":true'
I want to get a regex expression so I can capture the following:
group 1: location":
group 2: state":
group 3: active":
Since these 3 words all end with ":
I have tried the following regex, but it is not working:
"[(a-zA-Z)*]+":
I need to capture via 3 groups.
This works fine.
>>> re.findall("([a-zA-Z]+)\":", s)
['location', 'state', 'active']
Or you can use two steps to complete it. First, use re.findall
with [a-zA-Z]+\\":
to find 3 groups.
>>> re.findall("[a-zA-Z]+\":", s)
['location":', 'state":', 'active":']
Second, replace ":
with empty: r.replace("\\":", "")
>>> res = re.findall("[a-zA-Z]+\":", s)
>>> res
['location":', 'state":', 'active":']
>>> for r in res:
... r.replace("\":", "")
...
'location'
'state'
'active'
I was able to capture it with a simple (r'\\w+":')
>>> import re
>>> s = '"location":{"state":"WA"},"active": True'
>>> a = re.compile(r'\w+":')
>>> location, state, active = a.findall(s)
>>> location, state, active
('location":', 'state":', 'active":')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.