User
User

Reputation: 603

Regex for detect key and value

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

Answers (3)

Chase
Chase

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

Wiktor Stribiżew
Wiktor Stribiżew

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 quote

Upvotes: 1

Breezer
Breezer

Reputation: 10490

"([A-Z]*?)""(.*?([0-9]|[a-z]{1}?).*?)"

should work like a charm

Upvotes: 0

Related Questions