We’ve got an iRule that contains the following code. It works fine but I find the repetition offensive.
elseif {[HTTP::uri] contains "fx1"} {
use pool Xyzzy
}
elseif {[HTTP::uri] contains "fx2"} {
use pool Xyzzy
}
elseif {[HTTP::uri] contains "fx3"} {
use pool Xyzzy
}
elseif {[HTTP::uri] contains "fx4"} {
use pool Xyzzy
}
I thought this could be replaced with something like this
elseif {[HTTP::uri] matches "fx[1234]"} {
use pool Xyzzy
}
but it complains about"extra tokens at end of expression". So I tried this
elseif {[HTTP::uri] matches_regex "fx[1234]"} {
use pool Xyzzy
}
But that complains about an undefined procedure.
Can someone point me at a working example of the matches and/or matches_regex using a character range? The examples shown on the wiki are overly simple and therefore not very useful.
Braces may be used to prevent interpretation of strings surrounded by [] as commands, as you experienced (“undefined procedure”).
As long as there isn’t a variable inside the braces that requires expansion, you can use them in most cases instead of “”, and they can actually be more efficient, if harder to type & read.
Colin wrote an excellent tech tip recently about using {} with numbers to prevent the tcl interpreter from having to figure out if they are strings or not before performing the operation: DevCentral - An F5 Technical Community (Click here)
(Someday I’m going to have to go re-work all of my codeshare examples to take advantage of that optimization…)
Looks like Joe fixed the character display substitution issue - thanks Joe!
Great points, hoolio. Definitely more optimal approach. I didn’t realize you could use charsets with either string match or switch.
I just came back to point out that for the regex with braces, you can instead escape the [] with a backslash to prevent the interpreter from trying to execute as a command: