gpt4 book ai didi

regex - 如何使用正则表达式获取第三个数字短语 ip address(v4)

转载 作者:行者123 更新时间:2023-12-04 05:25:19 25 4
gpt4 key购买 nike

我想从 IP 地址(v4)中获取第三个数字
只使用正则表达式。

这里有很多ips

172.23。 34 .159
172.23。 34 .51
211.234。 202 .246
147.6。 41 .120
172.23。 34 .171
10.225。 149 .118
172.23。 34 .64

这可能吗?

最佳答案

这应该至少适用于 :

\b\d{1,3}\.\d{1,3}\.\K\d{1,3}(?=\.\d{1,3}\b)

或者如果你想要更强大的东西:
\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.\K(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?=\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)

你可以试试
$ re='\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.\K(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?=\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b)'
$
$ perl -lne 'print $& if /'"$re"'/' file.txt
34
34
202
41
34
149
34
$
$ grep -oP "$re" file.txt
34
34
202
41
34
149
34

关于regex - 如何使用正则表达式获取第三个数字短语 ip address(v4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13262421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com