我正在寻找IP字符串中的文本块,通常会将IP地址除以回车符。例如...用于字符串的Powershell正则表达式随机划分回车
e c t i o n
1 9 2
. 1 6 8 . 1 . 2 5 0 S t a t i o n
我想匹配整个IP地址。我已经尝试做一个积极的前瞻/ lookbehind像...
(?<=1 9 2)\S+?(?=2 5 .)
有什么想法吗?
我正在寻找IP字符串中的文本块,通常会将IP地址除以回车符。例如...用于字符串的Powershell正则表达式随机划分回车
e c t i o n
1 9 2
. 1 6 8 . 1 . 2 5 0 S t a t i o n
我想匹配整个IP地址。我已经尝试做一个积极的前瞻/ lookbehind像...
(?<=1 9 2)\S+?(?=2 5 .)
有什么想法吗?
回车符只是另一个空白字符(\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+'
((?:(?:[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
----------------------------------------------------------------------
真棒解决方案和故障。我看到它在您提供的regex101演示中工作。它看起来像它是在修改器gmix的PHP代码下构建的。不幸的是,这些在PowerShell中似乎并不可用(从我能说的)。尽管这里有很多有用的信息。 –
对不起,表达式在演示结尾处有一些额外的空格。随着这些空间被移除并且放下'mix'选项,演示就可以运行。在PowerShell中,'g'全局选项默认是启用的。 –
这并拉出IP和更进了一步比我一直在寻找。我最终正在寻找'$ Match.Matches.Value'的返回值,并将从中取代它。谢谢! –