INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

String Manipulation (RegEx)

How do I extract values from a given value? by onpnt
Posted: 24 May 03 (Edited 24 May 03)

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>
<%
' 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
'---------------------------------------------------

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

'---------------------------------------------------
' 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)
'---------------------------------------------------

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

' declare variables needed
Dim strNum, strStr
Dim strSuf, strNam
Dim x, ErrorVal
Dim MyStr

' we hard code the address value here but
' most likely you will bring it in from a DB
' or user entered value

MyStr = "555-22 S. Perry Ln."

' first order is to strip the apartment value
' as this is not needed in our output

MyStr = RegExpRep("(-)\d+\s",MyStr, " ")

' set x (counter) to 0
' I like to do this for a clean
' no questions asked script although it is
' not needed

x = 0


' here is our main functionality.  first we split the value by the spaces
' this gives us the seperated values to perform our testing by.

MyArray = Split(MyStr," ")

' 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.

Do While x <= UBound(MyArray)
        If RegExpTest("^\d{3}$",MyArray(x)) Then
        ' regex = beginning of string(^)numeric value 3 instances only(\d{3})end of string($)
        ' if found give value to Number variable

            strNum = MyArray(x)  

        ElseIf RegExpTest("^\w{1}\.*$",MyArray(x)) Then
        ' 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

            strDir = MyArray(x)

        ElseIf RegExpTest("^\w{2}\.*$",MyArray(x)) Then
            strSuf = MyArray(x)
        ' 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

        
        ElseIf RegExpTest("([a-zA-Z]$)",MyArray(x)) Then
            strNam = MyArray(x)
        ' regex = cahracter only([a-zA-Z]) end of string($)
        ' if found give value to Number variable

        
        Else
            ErrorVal = MyArray(x) & " Value not recognized"
        ' if no match set no value matching criteria
        End If
' increment counter through array
x = x + 1
Loop

' write results to screen
response.write strNum & "=Number<br>" & strDir & "=Direction<br>" & strNam
response.write "=Name<br>" & strSuf & "=Suffix"

%>
</head>
</html>

Happy Coding!

Back to Microsoft: ASP (Active Server Pages) FAQ Index
Back to Microsoft: ASP (Active Server Pages) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close