我正在寻找一个文本块中的IP字符串,该字符串通常将IP地址除以回车符。例如......
e c t i o n
1 9 2
. 1 6 8 . 1 . 2 5 0 S t a t i o n
我想匹配整个IP地址。我试过做一个积极的向前看/看起来好像......
(?<=1 9 2)\S+?(?=2 5 .)
有什么想法吗?
答案 0 :(得分:3)
回车只是另一个空格字符(\s
)。
你可以使用1-3个数字的非捕获组((?:pattern)
),然后用空格来匹配整个ip:(?:(?:(?:\d\s+){1,3})\.\s+){3}(\d\s+){0,2}\d
$string = @"
e c t i o n
1 9 2
. 1 6 8 . 1 . 2 5 0 S t a t i o n
"@
$Match = $string |Select-String '(?:(?:(?:\d\s+){1,3})\.\s+){3}(\d\s+){0,2}\d'
$IP = $Match.Matches.Value -replace '\s+'
答案 1 :(得分:1)
((?:(?:[0-9](?:\s[\n\r]|[\r\n]\s|\s)){1,3}(?:\.\s[\n\r]?|\.[\n\r]\s)){3}(?:[0-9](?:\s[\n\r]|[\r\n]\s|\s)){1,3})
此正则表达式将执行以下操作:
现场演示
https://regex101.com/r/sG8qS5/3
示例文字
IP address 1 1 9 2
. 1 6 8 . 1 . 2 5 0 S t a t i o n
e c t i o n
IP address 2 1 9 2 . 1 6
8 . 1 . 2 5 0 S t a t i o n
IP address 3 1 9 2 . 1 6
8 . 1 . 2
5 0 S t a t i o n
样本匹配
MATCH 1
1. [16-43] `1 9 2
. 1 6 8 . 1 . 2 5 0 `
MATCH 2
1. [123-150] `1 9 2 . 1 6
8 . 1 . 2 5 0 `
MATCH 3
1. [199-227] `1 9 2 . 1 6
8 . 1 . 2
5 0 `
NODE EXPLANATION
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
(?: group, but do not capture (3 times):
----------------------------------------------------------------------
(?: group, but do not capture (between 1
and 3 times (matching the most amount
possible)):
----------------------------------------------------------------------
[0-9] any character of: '0' to '9'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and "
")
----------------------------------------------------------------------
[\n\r] any character of: '\n' (newline),
'\r' (carriage return)
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[\r\n] any character of: '\r' (carriage
return), '\n' (newline)
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and "
")
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and "
")
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
){1,3} end of grouping
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
[\n\r]? any character of: '\n' (newline),
'\r' (carriage return) (optional
(matching the most amount possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
[\n\r] any character of: '\n' (newline),
'\r' (carriage return)
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
){3} end of grouping
----------------------------------------------------------------------
(?: group, but do not capture (between 1 and
3 times (matching the most amount
possible)):
----------------------------------------------------------------------
[0-9] any character of: '0' to '9'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
[\n\r] any character of: '\n' (newline),
'\r' (carriage return)
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[\r\n] any character of: '\r' (carriage
return), '\n' (newline)
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
){1,3} end of grouping
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------