Reputation: 603
I need a regex to detect the key and value in a text file, The key will be uppercase and the value will contain at least one lower case or digit
E.g.: "ADDRESS""32 street 90. ETC""AGE""13""CLASS""ABC" ....
Regex:
"[A-Z]+"`"((.*\d.*)|(.*[a-z]))"`
"[A-Z]+" to match the key
"((.*\d.*)|(.*[a-z]))" to match the value
But the value match the first value till the file end. Any ideas ?
Note: The regex should match ADDRESS and AGE only not CLASS
Upvotes: 2
Views: 422
Reputation: 5615
Try this regex-
"([A-Z]+)""([\w \.]+)"
This assumes the key can only contain uppercase letters ([A-Z]
) and the value can only contain word characters (\w
), spaces (
), and a literal dot \.
.
Check out the demo
Use it to construct a dictionary using-
>>> {k:v for k, v in re.findall(r'"(\w+)""([\w \.]+)"', '"ADDRESS""32 street 90. ETC""AGE""13"')}
{'ADDRESS': '32 street 90. ETC', 'AGE': '13'}
Upvotes: 0
Reputation: 626926
You can use
"([A-Z]+)""([^"]*[a-z0-9][^"]*)"
See this regex demo
Details
"
- a double quote([A-Z]+)
- Group 1: one or more uppercase letters""
- two "
chars([^"]*[a-z0-9][^"]*)
- Group 2:
[^"]*
- zero or more chars other than "
[a-z0-9]
- a lowercase letter or digit[^"]*
- zero or more chars other than "
"
- a double quoteUpvotes: 1