使用 TCL 剥离部分 URI 路径

Strip part of URI path with TCL

我对编程和 TCL 还很陌生。我正在研究利用 tcl 的 F5 iRules。

基本上我需要做的是去掉下面我的 URI 路径的第一部分 (/Version_13.0.001/): /Version_13.0.001/hs/user/123

将最终结果 URI 设为: /hs/user/123

以下是我的基本逻辑,我如何将其合并到我的规则中?

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

您可以使用 split or file split to break apart the path, remove the dirname at index 1, and then joinfile join

但是,进行正则表达式搜索和替换似乎更直接:

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

在这里,我们在字符串的开头找到“/Version_13”后跟非斜杠字符,并将其替换为空字符串。

或者使用 string 命令查找第二个斜杠的索引,并从那里开始获取子字符串:

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