2016-06-14 34 views

回答

2

回车符只是另一个空白字符(\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

这并拉出IP和更进了一步比我一直在寻找。我最终正在寻找'$ Match.Matches.Value'的返回值,并将从中取代它。谢谢! –

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})

Regular expression visualization

这个正则表达式将执行以下操作:

  • 匹配子,看起来像IP地址
  • 允许IP地址在多行

要跨越例

现场演示

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 
---------------------------------------------------------------------- 
+0

真棒解决方案和故障。我看到它在您提供的regex101演示中工作。它看起来像它是在修改器gmix的PHP代码下构建的。不幸的是,这些在PowerShell中似乎并不可用(从我能说的)。尽管这里有很多有用的信息。 –

+1

对不起,表达式在演示结尾处有一些额外的空格。随着这些空间被移除并且放下'mix'选项,演示就可以运行。在PowerShell中,'g'全局选项默认是启用的。 –