2017-04-25 41 views
3

我与这个字符串正则表达式:独立结肠逗号(IP:PORT)

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 

转换为

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:端口)几个小时,但我想不出出来!尝试像(?:\.\d+)(?<=\d)\:正则表达式,但它不起作用。
我想使用powershell -replace运算符。

+1

'\ d + \ \ d + \ \ d + \ \ d +(:)\ d +'匹配。 (并捕获)IP和端口之间的':'。这有帮助吗? – Maroun

+0

@WiktorStribiżew我试过,因为文本文件是大型的日志文件,它使这个过程更长! – HBS

+0

@MarounMaroun在powershell中,它匹配所有ip:port,我不能使用替换运算符 – HBS

回答

2

你可以使用

$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' 

输出:

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 

这里,

  • (\d+(?:\.\d+){2,3}) - 比赛1+位数字2只或3出现一个.,随后用1 +数字(组1,$1
  • : - 冒号
  • (\d+,) - 1+数字和,(第2组,$2