Search code examples
tclf5

Strip part of URI path with TCL


I'm fairly new with programming and TCL. I'm working on F5 iRules which utilize tcl.

Essentially what I need to do is strip out the first portion (/Version_13.0.001/) of my URI path below: /Version_13.0.001/hs/user/123

Making the end result URI to: /hs/user/123

Below is the basic logic I have, how would I incorporate this into my below irule?

  if { ([HTTP::path] contains "Version_13") } {
    pool version_13_pool }

Solution

  • You could use split or file split to break apart the path, remove the dirname at index 1, and then join or file join.

    However, it seems more straightforward to do a regular expression search and replace:

    set path "/Version_13.0.001/hs/user/123"
    set newpath [regsub {^/Version_13[^/]*} $path ""]
    puts $newpath     ; # => /hs/user/123
    

    Here, we're finding, at the beginning of the string, "/Version_13" followed by non-slash characters, and replacing that with an empty string.

    Or perhaps use the string command to find the index of 2nd slash, and take the substring starting from there:

    set newpath [string range $path [string first / $path 1] end]