I am trying to manipulate a text from SNMP sysDescr.0 output with Python 3, I need to use a dictionary that contains the patterns and their replacements as follows:
myDict = {
r' \(\/sw.+': '',
r' \(\/ws.+$': '',
r'Compiled on.{36}': '',
r'Ruckus Wireless, Inc. ': '',
r'Brocade Communications Systems, Inc. ': '',
r' Switch': '',
r', ROM': ' - ROM',
r' revision': 'revision',
r' IronWare': 'IronWare'
}
I found belowcode here but the first three patterns in the dictionary are not working, while the rest are OK, I don't know why:
def multiple_replace(myDict, text):
regex = re.compile(r'(%s)' % '|'.join(map(re.escape, myDict.keys())))
return regex.sub(lambda mo: myDict.get(mo.group(), mo.group()),text)
How can I modify the above function to be able to correctly run RegEX for the first three patterns? I tried most similar solutions here but non of them was able to handle the first three patterns.
My simple version is below, but I am really interested to see how the first solution should be modified to work correctly as I am new to python anyway:
def multiple_replace(myDict, text):
for key, val in myDict.items():
if re.search(key, text):
text = re.sub(key, val, text)
return text
Here is an example of the output:
HP J9856A 2530-24G-2SFP+ Switch, revision YA.16.05.0004, ROM YA.15.20 (/ws/swbuildm/rel_venice_qaoff/code/build/lakes(swbuildm_rel_venice_qaoff_rel_venice)) (Formerly ProCurve),.1.3.6.1.4.1.11.2.3.7.11.166
ProCurve J9088A Switch 2610-48, revision R.11.122, ROM R.10.06 (/sw/code/build/nemo),.1.3.6.1.4.1.11.2.3.7.11.77
Ruckus Wireless, Inc. ICX7250-48-HPOE, IronWare Version 08.0.70aT211 Compiled on Jan 18 2018 at 04:21:25 labeled as SPS08070a,.1.3.6.1.4.1.1991.1.3.62.2.2.1.1
and what I need it to become:
HP J9856A 2530-24G-2SFP+,revision YA.16.05.0004 - ROM YA.15.20,.1.3.6.1.4.1.11.2.3.7.11.166
HP J9088A 2610-48,revision R.11.122 - ROM R.10.06,.1.3.6.1.4.1.11.2.3.7.11.77
ICX7250-48-HPOE,IronWare Version 08.0.70aT211 SPS08070a,.1.3.6.1.4.1.1991.1.3.62.2.2.1.1
Honestly I have no idea which is better or faster, your input is appreciated.
thanks
map(re.escape, myDict.keys())escapes the strings, and corrupts the regex patterns.r' \(\/sw.+\)'andr' \(\/ws.+\)'. See ideone.com/iXaHBt