Trim parenthesis via tcl

268 Views Asked by At

I have the following URL and would like to remove the parentheses by using the iRule (TCL):

http://ing1.example.com:32100/test1/test1.json/Streams(Type_4000000)

when HTTP_REQUEST {
  if { ([HTTP::host] eq "ing1.example.com") or ([HTTP::host] eq "ing2.example.com" ) and ([HTTP::uri] contains "Streams")}
      set NEW_URI [string trimleft [HTTP::uri] ( or )]
  }

is the above correct? thank you.

1

There are 1 best solutions below

0
On

To remove parentheses from anywhere in a string (URLs are just strings with some format constraints) you should use string map.

proc removeParens {string} {
    # Convert each parenthesis character into an empty string, removing it
    string map {( "" ) ""} $string
}

The regsub command could also be used, but would be slightly overkill for this. The string trim command won't work easily; that only removes characters from the two ends of the string, not the middle, so you'd need to split it up and join it and so on and that's way too much work if you don't need to do it!


Note that URLs have a syntax for quoting characters that could cause parentheses to still be present in some sense (despite not literally being there); whether this matters to you depends on what other processing you're doing. Generally speaking, it's important to be very careful with any and all URL handling.