daily pastebin goal
66%
SHARE
TWEET

Regex Search from text or url

ShadowTzu Nov 20th, 2014 234 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.Text.RegularExpressions
  2. Imports System.Net
  3.  
  4. Public Class RegexFound
  5.     Implements IDisposable
  6.  
  7.     Private Structure struct_Order
  8.         Public Index As Integer
  9.         Public Group As Integer
  10.         Public Pattern As String
  11.         Public Navigate As Boolean
  12.     End Structure
  13.     Private mOrder_List As List(Of struct_Order)
  14.  
  15.     Private Web As Net.WebClient
  16.     Private mCurrentData As String
  17.  
  18.     Public Sub New()
  19.         mOrder_List = New List(Of struct_Order)
  20.         Web = New WebClient
  21.         Web.Encoding = System.Text.Encoding.UTF8
  22.     End Sub
  23.  
  24.     Public Sub Start(Url As String)
  25.         mCurrentData = Web.DownloadString(Url)
  26.     End Sub
  27.     Public Sub Start_FromText(text As String)
  28.         mCurrentData = text
  29.     End Sub
  30.  
  31.     ''' <summary>
  32.     ''' Add regex
  33.     ''' </summary>
  34.     ''' <param name="Index">-1 = every match found or 0=first match, 1=second match, etc...</param>
  35.     ''' <param name="Group">create group with (parenthesis). group 0=no group, 1=first group</param>
  36.     ''' <param name="Pattern">regex pattern</param>
  37.     ''' <param name="Navigate">if the match is url you can download string from it</param>
  38.     ''' <remarks></remarks>
  39.     Public Sub Add(Index As Integer, Group As Integer, Pattern As String, Optional Navigate As Boolean = False)
  40.         Dim mNewOrder As struct_Order
  41.         mNewOrder.Index = Index
  42.         mNewOrder.Pattern = Pattern
  43.         mNewOrder.Navigate = Navigate
  44.         mNewOrder.Group = Group
  45.         mOrder_List.Add(mNewOrder)
  46.     End Sub
  47.  
  48.     Public Sub Clear()
  49.         mOrder_List.Clear()
  50.     End Sub
  51.  
  52.     ''' <summary>
  53.     ''' test pattern with the current data without save it to working text
  54.     ''' </summary>
  55.     ''' <param name="Pattern"></param>
  56.     ''' <returns></returns>
  57.     ''' <remarks></remarks>
  58.     Public Function Test(Pattern As String) As String
  59.         Dim Result As MatchCollection
  60.  
  61.         Result = Regex.Matches(mCurrentData, Pattern)
  62.         Dim data As String = Nothing
  63.         For i As Integer = 0 To Result.Count - 1
  64.             data &= i & ": " & Result(i).Value
  65.             If i < Result.Count - 1 Then data &= vbCrLf
  66.         Next
  67.         Return data
  68.     End Function
  69.  
  70.     ''' <summary>
  71.     ''' process every regex and return final data
  72.     ''' </summary>
  73.     ''' <returns></returns>
  74.     ''' <remarks></remarks>
  75.     Public Function Process() As String
  76.         Dim Result As MatchCollection
  77.         For i As Integer = 0 To mOrder_List.Count - 1
  78.             Result = Regex.Matches(mCurrentData, mOrder_List(i).Pattern)
  79.             If mOrder_List(i).Index = -1 Then
  80.                 mCurrentData = ""
  81.                 For j As Integer = 0 To Result.Count - 1
  82.                     mCurrentData &= Result(j).Groups(mOrder_List(i).Group).Value                
  83.                     If j < Result.Count - 1 Then mCurrentData &= vbCrLf
  84.                 Next
  85.             Else
  86.                 mCurrentData = Result(mOrder_List(i).Index).Groups(mOrder_List(i).Group).Value
  87.             End If
  88.  
  89.             If mOrder_List(i).Navigate Then
  90.                 mCurrentData = Web.DownloadString(mCurrentData)
  91.             End If
  92.         Next
  93.         Return mCurrentData
  94.     End Function
  95.  
  96. #Region "IDisposable Support"
  97.     Private disposedValue As Boolean
  98.  
  99.     Protected Overridable Sub Dispose(disposing As Boolean)
  100.         If Not Me.disposedValue Then
  101.             If disposing Then
  102.                 mOrder_List.Clear()
  103.                 mOrder_List = Nothing
  104.                 Web.Dispose()
  105.                 Web = Nothing
  106.             End If
  107.  
  108.            
  109.         End If
  110.         Me.disposedValue = True
  111.     End Sub
  112.  
  113.     Public Sub Dispose() Implements IDisposable.Dispose    
  114.         Dispose(True)
  115.         GC.SuppressFinalize(Me)
  116.     End Sub
  117. #End Region
  118.  
  119. End Class
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top