Advanced string manipulation with Regular Expressions
This script was written from a question asked at here in regards to string manipulation on a address. After hinking about what needed to be done to the value the realization that regular expressions would be the most efficient and best means of the needed output. This is of a advanced nature but is still a simple enough process that a beginner can learn from it and in the same order get some regular expression exposure.
Our input (string) was something like "555-22 S. Perry Ln." The intended output needed was four variable populations including
Number = 555
Direction = S.
Name = Perry
Suffix = Ln.
First thought a simple split and populate witht the rray.
The question came up however the lack of a value in the string. Say "555-22 Perry Ln." or 555-22 S. Ln.". This came to why we needed a test method in the functions to know what we had and where to give values respectivly.
The script
<html>
<head>
<%[color blue]
' pattern below is a test pattern for the complete string
' possible
'ptrn = "(^\d{3}\s\w{1}\.\s+[\w\s\.]+$)"
'---------------------------------------------------
' function one: this is the test function
' we pass two parameters to this function and its
' main purpose is to test the pattern (parameter 1)
' against the array value (parameter 2) to allocate
' the value to the correct variable
'---------------------------------------------------[/color]
Function RegExpTest(patrn, strng)
Dim regEx, retVal
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = False
retVal = regEx.Test(strng)
RegExpTest = retVal
Set regEx = Nothing
End Function
[color blue]'---------------------------------------------------
' function Two: this is the repalce function
' we pass three parameters to this function and its
' main purpose is to strip the apartment value "-22
' from the string due to no need of this value
' pattern = (parameter 1) value = (parameter 2)
' replace value = (parameter 3)
'---------------------------------------------------[/color]
Function RegExpRep(patrn, str, replStr)
Dim regEx
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
RegExpRep = regEx.Replace(str, replStr)
Set regEx = Nothing
End Function
[color blue]' declare variables needed [/color]
Dim strNum, strStr
Dim strSuf, strNam
Dim x, ErrorVal
Dim MyStr
[color blue]' we hard code the address value here but
' most likely you will bring it in from a DB
' or user entered value[/color]
MyStr = "555-22 S. Perry Ln."
[color blue]' first order is to strip the apartment value
' as this is not needed in our output[/color]
MyStr = RegExpRep("(-)\d+\s",MyStr, " ")
[color blue]' set x (counter) to 0
' I like to do this for a clean
' no questions asked script although it is
' not needed[/color]
x = 0
[color blue]' here is our main functionality. first we split the value by the spaces
' this gives us the seperated values to perform our testing by.[/color]
MyArray = Split(MyStr," ")
[color blue]' now we needed to determine what value is focused and populate the
' proper variable on that test. The conditional statement below has
' a important order of operations to it. we first need to check
' numeric value (Number)
' direction
' suffix
' then name.
' If we did not perform this test in this order the regular expression as
' written would populate the direction, suffix and name incorrectly given
' the similar nature of the values.[/color]
Do While x <= UBound(MyArray)
If RegExpTest("^\d{3}$",MyArray(x)) Then
[color blue]' regex = beginning of string(^)numeric value 3 instances only(\d{3})end of string($)
' if found give value to Number variable[/color]
strNum = MyArray(x)
ElseIf RegExpTest("^\w{1}\.*$",MyArray(x)) Then
[color blue]' regex = beginning of string(^)word character only one instance(\w{1})
' 0 or more instances of .(\.*)end of string($)
' if found give value to Number variable[/color]
strDir = MyArray(x)
ElseIf RegExpTest("^\w{2}\.*$",MyArray(x)) Then
strSuf = MyArray(x)
[color blue]' regex = beginning of string(^)word character only two instance(\w{2})
' 0 or more instances of .(\.*)end of string($)
' if found give value to Number variable[/color]
ElseIf RegExpTest("([a-zA-Z]$)",MyArray(x)) Then
strNam = MyArray(x)
[color blue]' regex = cahracter only([a-zA-Z]) end of string($)
' if found give value to Number variable[/color]
Else
ErrorVal = MyArray(x) & " Value not recognized"
[color blue]' if no match set no value matching criteria[/color]
End If
[color blue]' increment counter through array [/color]
x = x + 1
Loop
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.