When you use the -match operator, even with a regular expression pattern, the operation only returns the first match found:
PS C:\> $var="Sapien Press PowerShell TFM" PS C:\> $var -match "\w+" True PS C:\> $matches Name Value ---- ----- 0 Sapien PS C:\>
To match all instances of the pattern, you need to use the Regex object. In this example, notice that the match method returns all matches in $var:
PS C:\> $regex=[regex]"\w+" PS C:\> $regex.matches($var) Groups : {Sapien} Success : True Captures : {Sapien} Index : 0 Length : 6 Value : Sapien Groups : {Press} Success : True Captures : {Press} Index : 7 Length : 5 Value : Press Groups : {PowerShell} Success : True Captures : {PowerShell} Index : 13 Length : 10 Value : PowerShell Groups : {TFM} Success : True Captures : {TFM} Index : 24 Length : 3 Value : TFM PS C:\>
We create an object variable called $regex and cast it to a regex object specifying the regular expression pattern. We can now call the matches method of the regex object using $var as a parameter. The method returns all instances where the pattern matches, as well as where they were found in $var.
A more direct way to see all the matches is to use the Value property:
PS C:\> foreach ($i in $regex.matches($var)) {$i.value} Sapien Press PowerShell TFM PS C:\>
The results of $regex.matches($var) is a collection. Using a ForEach loop we can enumerate the collection and display the Value property for each item in the collection.
This object has several methods and properties with which you will want to become familiar:
PS C:\> $regex|get-member TypeName: System.Text.RegularExpressions.Regex Name MemberType Definition ---- ---------- ---------- Equals Method System.Boolean Equals(Object obj) get_Options Method System.Text.RegularExpressions.RegexOpt get_RightToLeft Method System.Boolean get_RightToLeft() GetGroupNames Method System.String[] GetGroupNames() GetGroupNumbers Method System.Int32[] GetGroupNumbers() GetHashCode Method System.Int32 GetHashCode() GetType Method System.Type GetType() GroupNameFromNumber Method System.String GroupNameFromNumber(Int32 GroupNumberFromName Method System.Int32 GroupNumberFromName(String IsMatch Method System.Boolean IsMatch(String input), S Match Method System.Text.RegularExpressions.Match Ma Matches Method System.Text.RegularExpressions.MatchCol Replace Method System.String Replace(String input, Str Split Method System.String[] Split(String input), Sy ToString Method System.String ToString() Options Property System.Text.RegularExpressions.RegexOpt RightToLeft Property System.Boolean RightToLeft {get;} PS C:\>
In order to see what the current value of $regex is, we need to use the ToString() method:
PS C:\> $regex.ToString() \w+ PC C:\>
IsMatch will return either TRUE or FALSE if any match is made:
PS C:\> if ($regex.IsMatch($var)) { >> write-host "found" ($regex.Matches($var)).Count "matches" >> } >> found 4 matches PS C:\>
In this example we check to see if IsMatch is TRUE. If it is TRUE, the number of matches found in the string will be displayed. By the way, the Count method is not a property of the $regex object, but the result of evaluating $regex.Matches($var), which returns a collection object:
PS C:\> ($regex.Matches($var)).gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True MatchCollection System.Object PS C:\>