For a regular expressions example, here's what my parsing script looks like in vbscript (I didn't know perl that well, so I decided to let my home automation program do all of the hard work):
'
' gOBiHAI
' Extracts the parameters out of an OBiHAI syslog packet (only tested on the OBi110).
' Parses sPkt and populates an array called aParms
' Returns the upper bound of aParms
' The i=0,2,4... index of aParms is a string indicating the packet type
' The i=1,3,5... index of aParms is the data pair to data type i-1
'
' Packet types followed by data values:
' State, followed by one of the following integers: 0 = Unknown,1 = OffHook,2=OnHook,3=Ringing
' Status, followed by a string containing miscellaneous information
' ICMState, followed by one of the following integers: 0 = Unknown,1 = OffHook,2=OnHook,3=Ringing
' CallerIDNumber, followed by number, CallerIDName, followed by name
' IMPORTANT NOTE: CallerIDNumber has a lower index than CallerIDName
' Therefore, CallerIDName will be processed last by OBi110.OnChangeOnNewData
function gOBiHAIParser(sPkt, aParms)
dim oRegEx, oMatches, oMatch
dim Tmp
dim bStatusFlag
'grab status packet
bStatusFlag = true
iCmdLength = len(sPkt)
set oRegEx = New RegExp
with oRegEx
.ignoreCase = true
.global = true
' Test for off hook status response
'
' Note offhook will also correspond to in-call
' incall appears in syslog following a call being answered
' if we didn't do this, status would be stuck on "Ringing" during the entire call
' until the phone is hung up.
.pattern = "((offhook)|(off hook)|(state=incall)|(state:incall))"
set oMatches = .Execute(sPkt)
if oMatches.Count = 1 then
' Confirmed it is a valid off hook packet
redim aParms(1)
aParms(0) = "State"
aParms(1) = 1
'do not grab status packet
bStatusFlag = false
end if
' Test for on hook status response
.pattern = "((onhook)|(on hook))"
set oMatches = .Execute(sPkt)
if oMatches.Count = 1 then
' Confirmed it is a valid on hook packet
redim aParms(1)
aParms(0) = "State"
aParms(1) = 2
'do not grab status packet
bStatusFlag = false
end if
' Test for on ring status
.pattern = "((incoming new call)|(start ring))"
set oMatches = .Execute(sPkt)
if oMatches.Count = 1 then
' Confirmed it is a valid on hook packet
redim aParms(1)
aParms(0) = "State"
aParms(1) = 3
'do not grab status packet
bStatusFlag = false
end if
' Test for caller id data
' I assume that the OBi110 will dump name and number in the same packet
' and that a number will always be present, but a name may be blank...
.pattern = "(cid)"
set oMatches = .Execute(sPkt)
if oMatches.Count = 1 then
' Confirmed it is a valid on CID packet, now find number
.pattern = "([0-9]{7,})"
set oMatches = .Execute(sPkt)
if oMatches.Count > 0 then
redim aParms(3)
aParms(0) = "CallerIDNumber"
aParms(2) = "CallerIDName"
if oMatches.Count = 1 then
aParms(1) = oMatches(0)
' Now parse name information
' If name is less than two characters "Unknown Caller" is returned.
.pattern = "('.{2,}')"
set oMatches = .Execute(sPkt)
if oMatches.Count = 0 then
aParms(3) = "Unknown Caller"
else
aParms(3) = oMatches(0)
end if
elseif oMatches.Count = 2 then
' Handles case where a packet containing a phone number in the single quotes is received
' e.g. [SLIC] to deliver: CID '1235551234' 1233334567
aParms(1) = oMatches(1)
aParms(3) = "Unknown Caller" ' Don't return a number as name!
end if
end if
' Do not grab status packet
bStatusFlag = false
end if
' If no other packets are found, populate "Status" information
if bStatusFlag then
' Report Status info from FXS (foreign exchange server) packet
.pattern = "(<7> fxs:)"
set oMatches = .Execute(sPkt)
if oMatches.Count = 1 then
' Confirmed it is an FXS packet
redim aParms(1)
aParms(0) = "Status"
aParms(1) = .Replace(sPkt, "")
end if
bStatusFlag = false
end if
end with
set oRegEx = nothing
set oMatches = nothing
set oMatch = nothing
gOBiHAIParser = ubound(aParms)
end function