简体   繁体   中英

Find replace all strings expect ones that match regex

i am currently converting all ' to a " using the following code:

    #Converts ' to "
    lines = []
    replacements = {"'":'"'}

    with open('netstat_data_IP_formatted.json') as infile:
        for line in infile:
            for src, target in replacements.iteritems():
                line = line.replace(src, target)
            lines.append(line)
    with open('netstat_data_IP_formatted.json', 'w') as outfile:
        for line in lines:
            outfile.write(line)

and this is working fine, however i want to select all ports and remove all formatting around the ports, so would using a regex like this work and not pick up the IP addresses too ?,

^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$

then how can i remove the formatting around the port numbers, or is there an easier way to how i am making this ?

So this is the input

{'l_port': '48856', 'r_host': '95.211.210.72', 'r_port': '443', 'state': 'ESTAB$
{'l_port': '443', 'r_host': '37.218.247.217', 'r_port': '35805', 'state': 'TIME$
{'l_port': '48662', 'r_host': '95.211.210.72', 'r_port': '443', 'state': 'ESTAB$
{'l_port': '51316', 'r_host': '91.194.90.103', 'r_port': '443', 'state': 'ESTAB$

this is how it is after the script run

{"l_port": "48698", "r_host": "95.211.210.72", "r_port": "443", "state": "ESTAB$
{"l_port": "40406", "r_host": "178.62.252.82", "r_port": "443", "state": "TIME_$
{"l_port": "443", "r_host": "60.191.48.203", "r_port": "58220", "state": "SYN_R$
{"l_port": "36058", "r_host": "37.252.185.87", "r_port": "443", 'state': 'TIME_$

This is how i want it

{"l_port": 48698, "r_host": "95.211.210.72", "r_port": 443, "state": "ESTAB$
{"l_port": 40406, "r_host": "178.62.252.82", "r_port": 443, "state": "TIME_$
{"l_port": 443, "r_host": "60.191.48.203", "r_port": 58220, "state": "SYN_R$
{"l_port": 36058, "r_host": "37.252.185.87", "r_port": 443, 'state': 'TIME_$

All you need is to match digits with quotes but no periods:

re.sub("'([0-9]+)'",'\\1',line)

(Swap the quotes if you replace them first, of course.)

That said, if you need to do anything any more sophisticated you should be using the standard json package to actually parse the file and then format the data however you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM