2016-06-14 36 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'全局選項默認是啓用的。 –