[VB6] Using Regular Expression in String


Deskripsi
     Using Regular Expression or Regex in VB is very simply , we just need project refference , string we want to get and then the pattern.

Howto
1.  Project > Components > Microsoft VBscript Regular Expressions 5.5
2.  Write code below
'Create variable
Dim html as string
Dim myRegExp As regexp ' for create object
Dim myMatches As MatchCollection ' collection matches string (raw)
Dim myMatch As Match ' matches string

Set myRegExp = New regexp
myRegExp.IgnoreCase = True 'case insensitive
myRegExp.Global = True
' create your pattern , for example i use pattern that grab some string in web page source using regex.
myRegExp.Pattern = "<td style=" & """height:50px;""" & ">(.*)</td>"

'execute raw string, for example in html page source i have raw string
Set myMatches = myRegExp.Execute(html)

'to get string that matches with pattern
MsgBox myMatches.Count

For Each myMatch In myMatches   'get the match string.
      msgbox myMatch.Value
Next

*tips : if you still confuse create the pattern for regex , you can learn from www.regexpal.com to test your regex, just input raw string and create the pattern.
the pattern which match raw string will be highlight with a color, good website i think...
it save my time create a regex :)



Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih