Sign in to follow this  

TCL Regular Expressions

This topic is 4202 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites

This topic is 4202 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this