Overview
  Next

The Regex library allows you to use Regular Expressions.
Regular expressions are special patterns that describe very powerful search patterns.
This help file does not cover regular expressions. It only covers Basic4ppc implementation of regular expressions.
There are many tutorials and resources about regular expressions online.
One such site: http://www.regular-expressions.info/
It is highly recommended for every developer who is working with strings / texts to learn the basics of regular expressions.

The Regex library includes two types of objects, Regex and Match.
The Regex object holds and activates the regular expression pattern.
A Regex object can be activated on any number of strings.
The Match object represents a specific match and it contains the actual string that was matched along with other properties and methods.


Example:
'Regex1 is a Regex object and Match1 is a Match object.
Sub Globals

End Sub

Sub App_Start
      Match1.New1
      TrimTrailingAndLeadingSpaces
      CapitalizeFirstLetterInSentence
      CheckValidEmail("support.basic4ppc.com")
      CheckValidEmail("support@basic4ppc.com")
End Sub

Sub CheckValidEmail (Email)
      'The pattern was copied from this site: www.regular- expressions.info
      Regex1.New2("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$",True,False) 'We are using case insensitive regex.
      If Regex1.IsMatch(Email) Then
            Msgbox(Email & " is a valid address")
      Else
            Msgbox(Email & " is not a valid address","",cMsgboxOK,cMsgboxHand)
      End If
End Sub

Sub TrimTrailingAndLeadingSpaces
      Regex1.New1("(?<=^\s*)\w.*\w|\w(?>=\s*)") 'We are using look behind and look ahead in this pattern.
      Match1.Value = Regex1.Match("   Some string with spaces     ")
      Msgbox(Match1.String)
End Sub

Sub CapitalizeFirstLetterInSentence
      Regex1.New1("(?<=\.\s*|^)(\w)(\w*)") 'We are using two groups in this pattern.
      s = "this is a long string. the brown fox jumped over the fence. regex are much simpler than they first seem."
      Match1.Value = Regex1.Match(s)
      Do While Match1.Success
            s = StrRemove(s,Match1.Index,Match1.Length)
            s = StrInsert(s,Match1.Index,StrToUpper(Match1.GetGroup(1)) & Match1.GetGroup(2))
            Match1.Value = Match1.NextMatch 'Step to the next match.
      Loop
      Msgbox(s)
End Sub