REGEX: single semicolon (IP: PORT)

I am struggling with converting this string

date,time,mac_address,source_ip:source_port,dst_ip:dst_port,method,url  

2017-04-01,00:01:03,00:10:f3:3f:fe:f2,192.168.2.62:28741,172.20.0:80,GET,http://www.website.com

      

to

2017-04-01,00:01:03,00:10:f3:3f:fe:f2,192.168.2.62,28741,172.20.0,80,GET,http://www.website.com

      

(ip: port split) for hours, but I couldn't figure it out! tried regex for example (?:\.\d+)(?<=\d)\:

but it doesn't work.
I want to use powershell operator -replace

.

+3


source to share


1 answer


you can use

$s = "2017-04-01,00:01:03,00:10:f3:3f:fe:f2,192.168.2.62:28741,172.20.0:80,GET,http://www.website.com"
$s -replace "(\d+(?:\.\d+){2,3}):(\d+,)", '$1,$2'

      

Output:



2017-04-01,00:01:03,00:10:f3:3f:fe:f2,192.168.2.62,28741,172.20.0,80,GET,http://www.website.com

      

Here

  • (\d+(?:\.\d+){2,3})

    - matches 1 + digits with 2 or 3 occurrences .

    followed by 1 + digits (group 1, $1

    )
  • :

    - colon
  • (\d+,)

    - 1 + numbers and ,

    (group 2, $2

    )
+2


source







All Articles