2

I am doing a dictionary manipulation using regex in Python.I want to remove 1dc.com or 1DC.com or 1dc.COM or 1DC.COM from a dictionary item.

Example Dictionary -

{'system_name': 'a1pvdb092', 'fdc_inv_sa_team': 'X2AIX_GBS'}
{'system_name': 'W00000001.1DC.com', 'fdc_inv_sa_team': 'LAA.BRAZIL.AAA.WINDOWS\n'}
{'system_name': 'a10000048', 'fdc_inv_sa_team': 'X2AIX_NSS'}
{'system_name': 'a10000049', 'fdc_inv_sa_team': 'X2AIX_NSS'}

Expected Output -

['a1pvdb092']
['W00000001']
['a10000048']
['a10000049']

Script -

import re
from opswareConnect import data

for row in data:
    arg1 = [row["system_name"],]
    arg1 = re.sub('[.1DC.com]\\b', '', str(arg1))
    print arg1

Output from the script -

['a1pvdb092']
['WBPVAP001Dco']
['a10000048']
['a10000049']
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485

1 Answers1

6

Regex

The regular expression is \.1dc\.com. The backslash escapes the dot which normally matches any character rather than just a period.

Make the search case insensitive with the re.IGNORECASE flag.

Find and eliminate the target expression using re.sub().

Full solution

import re

data = [
    {'system_name': 'a1pvdb092', 'fdc_inv_sa_team': 'X2AIX_GBS'},
    {'system_name': 'W00000001.1DC.com', 'fdc_inv_sa_team': 'LAA.BRAZIL.AAA.WINDOWS\n'},
    {'system_name': 'a10000048', 'fdc_inv_sa_team': 'X2AIX_NSS'},
    {'system_name': 'a10000049', 'fdc_inv_sa_team': 'X2AIX_NSS'},
]

for row in data:
    sysname = row['system_name']
    print([re.sub(r'\.1dc\.com', '', sysname, flags=re.IGNORECASE)])

Output

['a1pvdb092']
['W00000001']
['a10000048']
['a10000049']
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485