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#. 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?
Two things:
You're currently piping an array of strings to Select-String
, and it'll process them one by one. Change this by using Get-Content -Raw
.
Secondly, you need to specify the -AllMatches
switch with Select-String
to get both instances:
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">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.