TCL Regular Expressions

Started by
4 comments, last by ArmitageIII87 17 years, 10 months ago
ok, so i'm new to TCL and am trying to write a small script to pick apart a string to make sure it is correct, i'll denote a variable(could be any number of characters) by a $ sign: I need it to make sure the string matches the following: "INVITE sip:($usr)@($IP_ADDR) SIP/2.0 (any number of $vars here) \r\n" can anyone give me some help in writing this script, thanks so much in advance, -Synt4x
Advertisement
ARe you trying to capture the $vars into TCL variables or are you just trying to ensure the string is valid accrding to some grammar?

Stephen M. Webb
Professional Free Software Developer

I'm gonna assume for a second that $usr, $IP_ADDR be resolved when your check is supposed to take place? I'm also gonna assume that you don't care about validating the "any number of $vars here" stuff. So maybe something like the below might work...not quite sure what it is you are going after. It sounds to me like you want to validate some incoming data against a regexp and then perform some error checking on it afterwards...but we all know what happens when you assume something...anyway...this should work.

set string_to_match {INVITE sip:Synt4x@127.0.0.1 SIP/2.0 ... \r\n}
regexp {INVITE sip:\((.*)\)@\((/d+)\.(/d+)\.(/d+)\.(/d+)\)\sSIP/2\.0.*} $string_to_match {} username ip1 ip2 ip3 ip4

...do error checking code on user name

...do error checking on ip values
check_ip_value{ $ip1 }

...possible helper function for checking an ip value...
proc check_ip_value{ ip_value } {
if { $ip_value > 255 || $ip_value < 0 } {
error "Invalid IP number $ip_value"
}

return 1
}
Thank you so much, but I just have a few questions,

1. What does /d do?
2. what does the ... do in the string_to_match
3. regexp {INVITE sip:\((.*)\)@\((/d+)\.(/d+)\.(/d+)\.(/d+)\)\sSIP/2\.0.*} $string_to_match {} username ip1 ip2 ip3 ip4

when using this, what does the second half do, I understand all of the first half(except the /d)
second half = {} username ip1 ip2 ip3 ip4

I'm not new to programming but I am VERY new to TCL, so sorry if any of these seem like foolish questions, Thanks again for the help!

-Synt4x
Quote:Original post by Synt4x

when using this, what does the second half do, I understand all of the first half(except the /d)
second half = {} username ip1 ip2 ip3 ip4

All the arguments after the first two are output variables for string captures.

Stephen M. Webb
Professional Free Software Developer

Yo,
1. /d+ is saying I want only digits and 1 or more of them.
2. the ... was put there to say that there could be anything there. Didn't quite know to to represent your "any number of $vars here" part of the string.
3. Bregma answered this question.

A really good resource for TCL, IMO, is TCL man pages as well as The TCL Bible

I use TCL quite frequently at work as well as in my homebrew game engine I'm working on. I find it sometimes really annoying with the syntax but the pretty darn good performance and good documentation keeps me using it. Hope this helps...

This topic is closed to new replies.

Advertisement