Home > Software engineering >  python regex not woking for multiple words (re)
python regex not woking for multiple words (re)

Time:06-23

Example

import re

data = 'DisplayName                     =   Umarex Steel Storm BB-Gun   ,'
pattern = r'(\w )\s*=\s*(\w )'

re.findall(pattern, data, re.MULTILINE)

Output

Wrong result: (DisplayName, Umarex)

Right result: (DisplayName, Umarex Steel Storm BB-Gun)

CodePudding user response:

You can use

(\w )\s*=\s*([^\s,](?:[^,]*[^\s,])?)

See the regex demo.

Details:

  • (\w ) - Group 1: one or more letters, digits, connector punctuation
  • \s*=\s* - a = char enclosed with zero or more whitespaces
  • ([^\s,](?:[^,]*[^\s,])?) - Group 2: a char other than whitespace and comma, then an optional sequence of any zero or more non-comma chars and then a char other than whitespace and comma.
  • Related