REGEX:用逗号分隔冒号(IP:PORT)

时间:2017-04-25 14:09:17

标签: regex

我正在努力转换这个字符串

echo "${platform:-Unknown}"

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

(将ip:port分开)几个小时,但我无法弄清楚!尝试了像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 这样的正则表达式,但它不起作用 我想使用powershell (?:\.\d+)(?<=\d)\:运算符。

1 个答案:

答案 0 :(得分: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