2017-09-23 92 views
-2

下面就只不过是两个串线,我匹配太Powershell的正则表达式来竖线之间的匹配

 6 |UDP  |ENABLED |  |15006 |010.247.060.120 | UDP/IP Communications | UDP/IP Communications GH1870 

10 |Gway |ONLINE |  |41794 |127.000.000.001 | DM-MD64x64 | DM-MD64x64 

下面是正则表达式我有这么远,但它只是底线匹配

(?i)(?<cipid>([\w\.]+))\s*\|\s*(?<ty>\w+)?\s*\|\s*(?<stat>[\w ]+)\s*\|\s*(?<devid>\w+)?\s*\|\s*(?<prt>\d+)\s*\|\s*(?<ip>([\d\.]+))\s*\|\s*(?<mdl>[\w-]+)\s*\|\s*(?<desc>.+) 

,我在想,如果我可以,只是每个垂直线之间的每一个字符相匹配的正则表达式,而不必明确地说什么是垂直线之间

谢谢所有

+1

'\ |(?:([^ \ |] *)\ |)*'' –

+1

进口CSV -headers 'cipid' ,'ty',... -delimiter'|' -path thatfile.txt'? – TessellatingHeckler

+0

$ array = $ line.Split(“|”)? –

回答

2

这通常工作。 (?:^|(?<=\|))[^|]*?(?=\||$)

https://regex101.com/r/KMNc47/1

格式化

(?:^| (?<= \|))   # BOS or Pipe behind 
[^|]*?      # Optional non-pipe chars 
(?= \| | $)     # Pipe ahead or EOS 

在这里它与空白装饰件和包括捕获基团。

(?:^|(?<=\|))\s*([^|]*?)\s*(?=\||$)

https://regex101.com/r/KMNc47/2

格式化

(?:^| (?<= \|))   # BOS or Pipe behind 
\s* 
([^|]*?)     # (1), Optional non-pipe chars 
\s* 
(?= \| | $)     # Pipe ahead or EOS 

这是一个捕捉收集配置。

(?:(?:^|\|)\s*([^|]*?)\s*(?=\||$))+

https://regex101.com/r/KMNc47/3

格式化

(?: 
    (?:^| \|)     # BOS or Pipe 
    \s* 
    ([^|]*?)     # (1), Optional non-pipe chars 
    \s* 
    (?= \| | $)     # Pipe ahead or EOS 
)+