guyrleech

Using regex to parse & tokenise strings in one operation

Nov 13th, 2019
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Original code from https://devblogs.microsoft.com/scripting/testing-rpc-ports-with-powershell-and-yes-its-as-much-fun-as-it-sounds/
  2. ## Showing here how to use regular expressions (regex) to match and tokenise strings in one operation via the $Matches built in variable
  3. ForEach ($strResult in $arrQuryResult)
  4. {
  5.     ## ncacn_ip_tcp:grl-dc03[49668]
  6.     If ($strResult -match "ip_tcp.*\[(\d+)\]" )
  7.     {
  8.         ## += on arrays/strings can be very slow! Use System.Collections.Generic.List & StringBuilder respectively
  9.         ## the () in the regex above denote a matching group where the first will go into $Matches[1] which is a built in hash table containing the matched groups
  10.         $arrPorts += $Matches[1]
  11.     }
  12. }
Add Comment
Please, Sign In to add comment