Skip to content Skip to sidebar Skip to footer

Match Word Before And After Colon

I have the following string: 'FIELDS--> FIELD1: \r\n FILED2: \r\nSOURCEFIELDS--> KEY1: VALUE1, KEY2: VALUE2, KEY3: VALUE3\r\' I want would want the following from the above

Solution 1:

You may use

re.findall(r'(\w+) *:(?: *([\w.-]+))?', x)

See the regex demo.

Details

  • (\w+) - Group 1: one or more word chars
  • *: - 0 or more spaces and a :
  • (?: *([\w.-]+))? - an optional sequence of
    • * - 0 or more spaces
    • ([\w.-]+) - Group 2: one or more word, . or - chars.

Post a Comment for "Match Word Before And After Colon"