Advertisement
codecaine

Regular expression custom functions and examples VBA

Aug 18th, 2018
595
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. Option Explicit
  2.  
  3.  
  4. Public Function regex_replace(data As String, pattern As String, Optional replace_with_str = vbNullString, Optional isGlobal As Boolean = True, Optional isCaseSensetive As Boolean = True, Optional isMultiLine As Boolean = True) As String
  5. 'replace string data with using a regex pattern
  6. Dim regex As Object
  7. On Error GoTo errHandler
  8. Set regex = CreateObject("vbScript.regExp")
  9. With regex
  10. .Global = isGlobal
  11. .ignoreCase = isCaseSensetive
  12. .MultiLine = isMultiLine
  13. .pattern = pattern
  14. regex_replace = .Replace(data, replace_with_str)
  15. End With
  16. exitSuccess:
  17. Set regex = Nothing
  18. Exit Function
  19. errHandler:
  20. Debug.Print Err.Description
  21. regex_replace = data
  22. Resume exitSuccess
  23. End Function
  24.  
  25. Public Function regex_pattern_count(data As String, pattern As String, Optional isGlobal As Boolean = True, Optional isCaseSensetive As Boolean = True, Optional isMultiLine As Boolean = True) As Long
  26. 'Count the number of Pattern matches in a string.
  27. 'if an error occurs -1 is returned
  28. Dim regex As Object
  29. Dim Matches As Object
  30.  
  31. 'Set up regular expression object
  32. On Error GoTo errHandler
  33. Set regex = CreateObject("vbScript.regExp")
  34. regex.pattern = pattern
  35. regex.Global = isGlobal
  36. regex.ignoreCase = isCaseSensetive
  37. regex.MultiLine = isMultiLine
  38. 'Retrieve all matches
  39. Set Matches = regex.Execute(data)
  40. 'Return the corrected count of matches
  41. regex_pattern_count = Matches.Count
  42. exitSuccess:
  43. Set regex = Nothing
  44. Exit Function
  45. errHandler:
  46. Debug.Print Err.Description
  47. regex_pattern_count = -1
  48. Resume exitSuccess
  49. End Function
  50.  
  51. Public Function regex_get_matches(data As String, pattern As String, Optional isGlobal As Boolean = True, Optional isCaseSensetive As Boolean = True, Optional isMultiLine As Boolean = True) As Collection
  52. 'returns a collection of pattern matches
  53. 'if an error occurs and empty collection is returned
  54. Dim regex As Object
  55. Dim Matches As Object
  56. Dim item As Object
  57. Dim container As New Collection
  58.  
  59. 'Set up regular expression object
  60. On Error GoTo errHandler
  61. Set regex = CreateObject("vbScript.regExp")
  62. regex.pattern = pattern
  63. regex.Global = isGlobal
  64. regex.ignoreCase = isCaseSensetive
  65. regex.MultiLine = isMultiLine
  66. 'Retrieve all matches
  67. Set Matches = regex.Execute(data)
  68. 'insert each match into container as string data
  69. For Each item In Matches
  70. container.Add (CStr(item))
  71. Debug.Print CStr(item)
  72. Next item
  73. exitSuccess:
  74. Set regex_get_matches = container
  75. Set container = Nothing
  76. Set regex = Nothing
  77. Exit Function
  78. errHandler:
  79. Debug.Print Err.Description
  80. Set regex_get_matches = New Collection
  81. Resume exitSuccess
  82. End Function
  83.  
  84.  
  85.  
  86. Sub regex_testing()
  87. Const data = "Today is a good day to code! 08-18-18"
  88.  
  89. 'get word count
  90. Debug.Print regex_pattern_count(data, "[a-z]+")
  91.  
  92. 'display words only
  93. Debug.Print regex_replace(data, "[^a-z\s]")
  94.  
  95. 'extract date
  96. Call regex_get_matches(data, "[0-9]+-[0-9]+-[0-9]+")
  97.  
  98. 'remove date
  99. Debug.Print regex_replace(data, "[0-9]+-[0-9]+-[0-9]+")
  100.  
  101. 'get only letters numbers and spaces
  102. Debug.Print regex_replace(data, "[^a-z\s]")
  103.  
  104. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement