Debug regex operation when matching any string


posted by Archdeacon
01-25-2012

Downloads: 55
File size: 5.6kB
Views: 267

Embed
Debug regex operation when matching any string
  1. function Debug-Regex
  2. <# 
  3. .SYNOPSIS 
  4. A very simple function to debug a Regex search operation and show any 'Match'  
  5. results. (V2.1 Export Alias db & some patterns, 28 Jan 2012). 
  6.  
  7. .DESCRIPTION 
  8. Sometimes it is easier to correct any regex usage if each match can be shown  
  9. in context. This function will show each successful result in a separate  
  10. colour, including the strings both before and after the match. Using the -F 
  11. switch will allow only the first of any matches to be returned. 
  12. Commonly used regex patterns may be added to this module, 'exported' and then  
  13. assigned to a variable before being passed to the function. (They can also be 
  14. used with the Select-String Cmdlet). 
  15.  
  16. .EXAMPLE 
  17. Debug-Regex '\b[A-Z]\w+' 'Find capitalised Words in This string' -first 
  18. Use the -F switch to return only the first match. Produces the following... 
  19.  
  20. MATCHES  ---------------<match>-------------------- 
  21. 1 [<Find> capitalised Words in This string
  22. .EXAMPLE 
  23. Debug-Regex '\b[0-9]+\b' 'We have to find numbers like 123 and 456 in this' 
  24. Produces the following... 
  25.  
  26. MATCHES  ---------------<match>-------------------- 
  27. 1 [We have to find numbers like <123> and 456 in this] 
  28. 2 [We have to find numbers like 123 and <456> in this] 
  29.  
  30. .EXAMPLE 
  31. PS >$a = Get-Variable regexDouble -ValueOnly 
  32. PS >db $a 'Find some double words words in this this string' 
  33. Assign the desired Regex string to $a and use this and the alias as parameters. 
  34.  
  35. .NOTES 
  36. Based on a suggestion from the book 'Mastering Regular Expressions' by J.Friedl
  37. page 429. 
  38.  
  39. .LINK 
  40.    Web Address Http://www.SeaStarDevelopment.BraveHost.com  
  41. #> 
  42.    param ([Parameter(mandatory=$true)][regex]$regex
  43.           [Parameter(mandatory=$true)][string]$string
  44.           [switch]$first
  45.    $m = $regex.match($string
  46.    if (!$m.Success) { 
  47.       Write-Host "No Match using Regex '$regex'" -Fore Yellow 
  48.       return 
  49.    
  50.    $count =
  51.    Write-Host "MATCHES  --------------------<" -Fore Yellow -NoNewLine 
  52.    Write-Host "match"                          -Fore White  -NoNewLine 
  53.    Write-Host ">--------------------"          -Fore Yellow 
  54.    while ($m.Success) { 
  55.       Write-Host "$count $($m.result('[$`<'))" -Fore Yellow -NoNewLine  
  56.       Write-Host "$($m.result('$&'))"          -Fore White  -NoNewLine  
  57.       Write-Host "$($m.result('>$'']'))"       -Fore Yellow  
  58.       if ($first) { 
  59.          return 
  60.       
  61.       $count++ 
  62.       $m = $m.NextMatch() 
  63.    
  64. # Use a CSV file for larger collections of variables if necessary. 
  65.  
  66. Set-Variable -Name regexDouble  -value '\b(\w+)((?:\s|<[^>]+>)+)(\1\b)'
  67.              -Description 'Find double word pairs within <>' 
  68. Set-Variable -Name regexNumbers -value '\b[0-9]+\b'
  69.              -Description 'Find only numbers in a string' 
  70.  
  71. New-Alias db Debug-Regex 
  72. Export-ModuleMember -Function Debug-Regex -Variable regex* -Alias db 

.SYNOPSIS
A very simple function to debug a Regex search operation and show any 'Match'
results.
.DESCRIPTION
Sometimes it is easier to correct any regex usage if each match can be shown
in context. This function will show each successful result in a separate
colour, including the strings both before and after the match. Using the -F
switch will allow only the first of any matches to be returned.

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.