fork download
  1. Imports System
  2. Imports System.Text.RegularExpressions
  3.  
  4. Public Class Test
  5. Public Shared Sub Main()
  6. Dim pattern As String = "^[ \t]* # beginning of line " & vbCrLf &
  7. "[#]define[ \t]+ # PAWN #define " & vbCrLf &
  8. "(?<name>[^\s\\;]+) # DEFINE_NAME " & vbCrLf &
  9. "[ \t]*(?:\\\r?\n[ \t]*)? # spaces and optional \ " & vbCrLf &
  10. "(?> # " & vbCrLf &
  11. " (?<value>(?> # DEFINE_VALUE " & vbCrLf &
  12. " [^\\\r\n;]+ | # Any char -except \ \n" & vbCrLf &
  13. " \\[^\r\n][^\\\r\n;]* # \ within value " & vbCrLf &
  14. " )+)? # (~unrolling the loop)" & vbCrLf &
  15. " (?:\\\r?\n[ \t]*)? # \ for new line " & vbCrLf &
  16. ")* # repeated for each line"
  17.  
  18. Dim re As Regex = new Regex( pattern, RegexOptions.Multiline Or
  19. RegexOptions.IgnorePatternWhitespace)
  20. Dim text As String = "#define DEFINE_NAME \" & vbCrLf &
  21. " DEFINE VALUE\" & vbCrLf &
  22. " CONTINUE VALUE" & vbCrLf &
  23. "#define TheName TheValue"
  24. Dim mNum As Integer = 0
  25. Dim matches As MatchCollection = re.Matches(text)
  26.  
  27. 'Loop Matches
  28. For Each match As Match In matches
  29. 'get name
  30. Dim name As String = match.Groups("name").Value
  31. Console.WriteLine("Match #{0} - Name: {1}", mNum, name)
  32.  
  33. 'get values (in each capture)
  34. Dim captureCtr As Integer = 0
  35. For Each capture As Capture In match.Groups("value").Captures
  36. 'loop captures for the Group "value"
  37. Console.WriteLine(vbTab & "Line #{0} - Value: {1}",
  38. captureCtr, capture.Value)
  39. captureCtr += 1
  40. Next
  41. mNum += 1
  42. Next
  43. End Sub
  44. End Class
Success #stdin #stdout 0.09s 24496KB
stdin
Standard input is empty
stdout
Match #0 - Name: DEFINE_NAME
	Line #0 - Value: DEFINE VALUE
	Line #1 - Value: CONTINUE VALUE
Match #1 - Name: TheName
	Line #0 - Value: TheValue