使用 POSIX BRE/ERE 正则表达式将 IP 地址提取为整个单词 [英] Extracing IP addresses as whole words with POSIX BRE/ERE regex
问题描述
我正在尝试通过正则表达式匹配在 traceroute
的输出中找到的 IP 地址.我不是要验证它们,因为假设 traceroute
有效是足够安全的(即不输出类似 999.999.999.999
的内容.我正在尝试以下正则表达式:
I am trying to match IP addresses found in the output of traceroute
by means of a regex. I'm not trying to validate them because it's safe enough to assume traceroute
is valid (i.e. is not outputting something like 999.999.999.999
. I'm trying the following regex:
([0-9]{1,3}.?){4}
我正在 regex101 中对其进行测试,它确实验证了 IP 地址.但是,当我尝试
I'm testing it in regex101 and it does validate an IP address. However, when I try
echo '192.168.1.1 foobar' | grep '([0-9]{1,3}.?){4}'
我一无所获.我错过了什么?
I get nothing. What am I missing?
推荐答案
您使用了 POSIX ERE 模式,但没有通过 -E
选项让 grep
使用POSIX ERE 风味.因此,grep
使用 POSIX BRE 代替,您需要转义 {n,m}
量词和 (...)
以使它们成为解析为特殊的正则表达式运算符.
You used a POSIX ERE pattern, but did not pass -E
option to have grep
use the POSIX ERE flavor. Thus, grep
used POSIX BRE instead, where you need to escape {n,m}
quantifier and (...)
to make them be parsed as special regex operators.
请注意,您需要对 .
进行转义,以便它只能匹配文字点.
Note you need to escape a .
so that it could only match a literal dot.
要使您的模式以您想要的方式与 grep
一起使用:
To make your pattern work with grep
the way you wanted you could use:
grep -E '([0-9]{1,3}\.?){4}' # POSIX ERE
grep '\([0-9]\{1,3\}\.\?\)\{4\}' # POSIX BRE version of the same regex
查看在线演示.
但是,这个正则表达式也将匹配一个由多个数字组成的字符串,因为 .
是可选的.
However, this regex will also match a string of several digits because the .
is optional.
你可以通过展开模式来解决它
You may solve it by unrolling the pattern as
grep -E '[0-9]{1,3}(\.[0-9]{1,3}){3}' # POSIX ERE
grep '[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}' # POSIX BRE
参见另一个演示.
基本上,它匹配:
[0-9]{1,3}
- 任何 ASCII 数字出现 1 到 3 次(\.[0-9]{1,3}){3}
- 出现 3 次:\.
- 文字.
[0-9]{1,3}
- 任何 ASCII 数字出现 1 到 3 次
[0-9]{1,3}
- 1 to 3 occurrences of any ASCII digit(\.[0-9]{1,3}){3}
- 3 occurrences of:\.
- a literal.
[0-9]{1,3}
- 1 to 3 occurrences of any ASCII digit
为了确保您只匹配有效的 IP,您可能需要使用 更精确的 IP 匹配正则表达式:
To make sure you only match valid IPs, you might want to use a more precise IP matching regex:
grep -E '\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]?)){3}\b' # POSIX ERE
请参阅此在线演示.
您可以进一步调整字边界(可以是
\<
/\>
或\b
)等You may further tweak it with word boundaries (can be
\<
/\>
or\b
), etc.要提取 IP 使用
-o
选项和grep
:grep -oE 'ERE_pattern' 文件
/grep -o 'BRE_pattern' 文件
.To extract the IPs use
-o
option withgrep
:grep -oE 'ERE_pattern' file
/grep -o 'BRE_pattern' file
.这篇关于使用 POSIX BRE/ERE 正则表达式将 IP 地址提取为整个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!