[英]Unable to match a multiline regex with Powershell (but it does work in C#)
Given: 鉴于:
PS D:\tmp> cat .\1.txt
<abc xyz="1"
def="xx">
xaxa
</abc>
<abc xyz="a">
PS D:\tmp>
What I was trying: 我正在尝试:
PS D:\tmp> cat .\1.txt | sls '(?m:)<abc[^>]+>'
<abc xyz="a">
PS D:\tmp> cat .\1.txt | sls '(?m:)<abc(?:[^>]|$)+>'
<abc xyz="a">
PS D:\tmp> cat .\1.txt | sls '(?m:)<abc(?:[^>]|\$)+>'
<abc xyz="a">
PS D:\tmp>
Now I know all the three variants work as expected in plain C#. 现在,我知道所有这三种变体在纯C#中都可以正常工作。 For example: 例如:
PS D:\tmp> [Text.RegularExpressions.Regex]::Matches($(cat 1.txt), '(?m:)<abc[^>]+>')
Groups : {<abc xyz="1" def="xx">}
Success : True
Captures : {<abc xyz="1" def="xx">}
Index : 0
Length : 27
Value : <abc xyz="1" def="xx">
Groups : {<abc xyz="a">}
Success : True
Captures : {<abc xyz="a">}
Index : 42
Length : 13
Value : <abc xyz="a">
PS D:\tmp>
So, I am curious - what am I doing wrong in pure Powershell that it does not work? 因此,我很好奇-在纯Powershell中我做错了什么而不能正常工作?
Two things: 两件事情:
You're currently piping an array of strings to Select-String
, and it'll process them one by one. 当前,您正在将字符串数组传递给Select-String
,它将逐个处理它们。 Change this by using Get-Content -Raw
. 通过使用Get-Content -Raw
更改此Get-Content -Raw
。
Secondly, you need to specify the -AllMatches
switch with Select-String
to get both instances: 其次,您需要使用Select-String
指定-AllMatches
开关以获取两个实例:
PS C:\> Get-Content .\1.txt -Raw |Select-String '(?m:)<abc[^>]+>' -AllMatches |Select -Expand Matches
Groups : {<abc xyz="1"
def="xx">}
Success : True
Captures : {<abc xyz="1"
def="xx">}
Index : 0
Length : 27
Value : <abc xyz="1"
def="xx">
Groups : {<abc xyz="a">}
Success : True
Captures : {<abc xyz="a">}
Index : 42
Length : 13
Value : <abc xyz="a">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.