0

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.

Colin Macleod
  • 4,222
  • 18
  • 21
user515576
  • 65
  • 6
  • What happened when you tried it? – user207421 Mar 04 '22 at 07:40
  • Tcl language documentation can be found at https://www.tcl-lang.org/man/tcl8.6/TclCmd/contents.htm - try reading it. – Colin Macleod Mar 04 '22 at 08:06
  • Just noticed the reference to iRule; this indicates that this code is intended to execute in an F5 network device. That environment is based on Tcl but provides extra facilities of its own. I will add the F5 tag. – Colin Macleod Mar 04 '22 at 09:16

1 Answers1

3

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.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215