I am trying to find all the strings I define in -Pattern in order. For example,
$string = "My car is Yellow but my car seat is White. I ate Yellow Banana, Yellow lemon and White peach."
Select-String -inputObject $string -Pattern 'Yellow', 'White'
I want the result to be like this in an array object in the order it finds in the string:
Yellow
White
Yellow
Yellow
White
CodePudding user response:
You can use the -AllMatches
parameter, however it doesn't look like it will utilize both patterns in this case. Adjusting the pattern might help.
($string | Select-String -AllMatches -Pattern 'Yellow|White').Matches.Value
CodePudding user response:
You can see the matches property in list view. You also need -allmatches.
Select-String -inputobject $string -Pattern 'Yellow', 'White' -AllMatches |
format-list
IgnoreCase : True
LineNumber : 1
Line : My car is Yellow but my car seat is White. I ate Yellow Banana, Yellow lemon and White peach.
Filename : InputStream
Path : InputStream
Pattern : Yellow
Context :
Matches : {0, 0, 0}
Select-String -inputobject $string -Pattern 'Yellow', 'White' -AllMatches |
% matches
Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 10
Length : 6
Value : Yellow
Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 49
Length : 6
Value : Yellow
Groups : {0}
Success : True
Name : 0
Captures : {0}
Index : 64
Length : 6
Value : Yellow