Parse String Using Regex


/ Published in: VB.NET
Save to your folder(s)



Copy this code and paste it in your HTML
  1. ''' <summary>
  2. ''' Parses string matching regular expression
  3. ''' </summary>
  4. ''' <param name="_string">String to be parsed</param>
  5. ''' <param name="_regularExpression">Regular expresion to match</param>
  6. ''' <returns>String array matching regular expression</returns>
  7. ''' <remarks></remarks>
  8. Public Function ParseByRegex(ByVal _string As String, ByVal _regularExpression As String) As String()
  9. Dim regex As New Regex(_regularExpression, RegexOptions.IgnoreCase Or RegexOptions.Singleline)
  10. Dim matches As Match = regex.Match(_string)
  11. If (matches.Success) Then
  12. Dim stringArray(matches.Groups.Count - 2) As String
  13. 'Skips 0 because regex.Match(0) is original string
  14. For i = 1 To matches.Groups.Count - 1
  15. stringArray(i - 1) = matches.Groups(i).ToString
  16. Next
  17. Return stringArray
  18. End If
  19. Return Nothing
  20. End Function

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.