0

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?

mark
  • 59,016
  • 79
  • 296
  • 580

1 Answers1

0

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">
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206