Forum Discussion
Best Practice: Regex vs number of if/else
I previously had a rule that had:
if {
$login == 1 &&
([string first "OP=help" [HTTP::header "location"]] == -1) &&
([string first "OP=token_login" [HTTP::header "location"]] == -1) &&
([string first "OP=error" [HTTP::header "location"]] == -1) &&
([string first "ERR_CODE=0x13212077 " [HTTP::header "location"]] == -1) &&
([string first "ERR_CODE=0x132120c8" [HTTP::header "location"]] == -1) &&
([string first "OP=acct_locked" [HTTP::header "location"]] == -1) } {
} {
..do something...
}
I then thought that this was a bit inefficient as you would have to traverse each condition before "doing something". I thought i would change this to a regex:
if { $login == 1 &&
([regexp "OP=help|OP=token_login|OP=error|ERR_CODE=0x13212077|ERR_CODE=0x132120c8|OP=acct_locked" [HTTP::header "location"]] == 0)
} {
..do something...
}
But reading some solutions on AskF5 and the iRule 101 Blogs it seems that F5 generally recommend avoiding regex for simple cases.
So I thought i would ask the masses....any experience or any advice on what is the best approach?
Thanks in advance,
NJ
- Patrick_Chang_7Historic F5 AccountWe say to avoid REGEX because it can be greedy and inefficient. In this case, I would say that it should be fine if [HTTP::header "location"] is not too long a string (say 30 characters or less).
- Naman_65674NimbostratusActually the string can get quite long....up to 180 characters if not more....
- Patrick_Chang_7Historic F5 AccountIn that case, I would stick with the original.
- spark_86682Historic F5 AccountIf your logic really looks like that (wanting to detect the absence of any of a number of strings in the same place), then you might also look into creating a class (data group) with your strings and using the class command, which might be even more efficient.
- Naman_65674NimbostratusThat actually might be a good idea spark, as i would be able to add the list quite easily.
- Naman_65674NimbostratusCan anyone advise on the way I have used matchclass above?
- Michael_YatesNimbostratusHi Naman,
if { [matchclass [HTTP::header "Location"] contains $::error_msgs ] } { ....do something... }
- Naman_65674Nimbostratus(Yes i am using v9.4.8)
Thanks for the tip. I noticed however that in the DevCentral wiki page on matchclass it said the following:
"Note that you should not use a $:: or :: prefix on the datagroup name when using the matchclass command (or in any datagroup reference) on 9.4.4 or later. For details, see theCMP compatibility page."So do we use instead of "$::"Thanks,Naman - nitassEmployeejust removing $::
if {[matchclass [HTTP::header "Location"] contains error_msgs] } { ....do something... }
- Michael_YatesNimbostratusHi Naman,
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com