Search code examples
pythonregexregex-group

Python regex named groups in special formated string


Objective:

  1. I have input string where it is separated by commas and key values in between
  2. I want to write regex with names groups so i can extract and replace values of each property

in string *keyword , property1 = ABC, property2 = 2 I want to find and replace value of property1 and property2 by name

for given string *keyword , property1 = ABC, property2 = 2 , result string should be *keyword , property1 = DEF, property2 = 10

See code below

import re

# find property1 in given string and replace its new value
property1 = 'DEF'
# find property2 in given string and replace its new value
property2 = '10'

line1 = '*keyword , property1 = ABC,  property2 = 2 '
line2 = '*keyword , property2 = 2,  property1 = ABC ' #property2 comes before proeprty1
line3 = '*keyword,property1=ABC,property2= 2' #same as line 1 but without spaces

regex_with_named_group = r'=\s*(.*)\s*,\s*property1=\s*(.*)\s*,'

line1_found = re.search(regex_with_named_group, line1)
line2_found = re.search(regex_with_named_group, line2)
line3_found = re.search(regex_with_named_group, line3)

if line1_found:
    print( line1_found.group('property1'), line1_found.group('property2') )

if line2_found:
    print( line2_found.group('property1'), line2_found.group('property2') )

if line3_found:
    print(line3_found.group('property1'), line3_found.group('property2'))

Solution

  • To achieve your goals, I suggest considering using the re.sub function.

    import re
    
    line0 = '*keyword , fruit=apple, juice= mango'
    line1 = '*keyword , property1 = ABC,  property2 = 2 '
    line2 = '*keyword , property2 = 2,  property1 = ABC ' #property2 comes before proeprty1
    line3 = '*keyword,property1=ABC,property2= 2' #same as line 1 but without spaces
    
    regex_with_named_group = re.compile(r'(?P<prop>\w+)(?P<map>\s*=\s*)(?P<val>\w+)')
    repl = {'fruit':'avocado', 'property1':170}
    for l in [line0, line1, line2, line3]:
        s = regex_with_named_group.sub(lambda m: m.group('prop') + m.group('map') +
                                                 (str(repl[m.group('prop')])
                                                  if m.group('prop') in repl
                                                  else m.group('val')), l)
        print(s)
    

    Result:

    *keyword , fruit=avocado, juice= mango
    *keyword , property1 = 170,  property2 = 2 
    *keyword , property2 = 2,  property1 = 170 
    *keyword,property1=170,property2= 2
    

    Demo.