Been searching but no luck so far looking for a iRule with a regex or some other method to normalize phone numbers from AD for use to pass to an SMS gateway. I have adapted the method here: DevCentral - An F5 Technical Community for our local Multitech SMS gateway and it does work fine with “unformatted” phone numbers like xxxxxxxxxxx, but management is not keen on my changing everyone’s mobile number to exclude those characters for readability. Basically, our AD has a mismatch of methods for phone numbers, some have dashes, some have spaces, some have parentheses around the area code, the EU numbers have dots instead of dashes, so xxx.xxx.xxxx. Has anyone tried this before? Any suggestions would be most welcomed as my iRule and regex -fu is not as strong as it should be. ![]()
Hey Mike, you might be able to cobble together what you’re looking for from this Stack thread.
Hi Jason,
Thanks for linking that, it looks potentially helpful but also dangerously complicated.
There is one suggestion in there which appears to me to be more helpful to our situation, which is to strip all non-numeric characters from the attribute and then use it as a variable, so I will talk to my colleague about it and see if we can figure out how to do that as that suggestion didn’t get posted with a regex to do it, but just as a suggestion.
Do you need regex for this?
set num1 "123.456.7890"
set num2 "123-456-7890"
set num3 "123 456 7890"
set num4 "(123) 456-7890"
log local0. [string map {"." "" " " "" "-" "" "(" "" ")" ""} $num1]
log local0. [string map {"." "" " " "" "-" "" "(" "" ")" ""} $num2]
log local0. [string map {"." "" " " "" "-" "" "(" "" ")" ""} $num3]
log local0. [string map {"." "" " " "" "-" "" "(" "" ")" ""} $num4]
Output:
1234567890
1234567890
1234567890
1234567890
Or instead of string map, you can strip non-numeric characters:
%proc justNumbers {pn} {
set nums "0 1 2 3 4 5 6 7 8 9"
set newNumber ""
foreach x [split $pn ""] {
if { !([lsearch $nums $x] == -1) } {
append newNumber $x
}
}
return $newNumber
}
% set x1 "1-800-632-6223"
1-800-632-6223
% set x2 "1.800.632.6223"
1.800.632.6223
% set x3 "1(800)632-6223"
1(800)632-6223
% justNumbers $x1
18006326223
% justNumbers $x2
18006326223
% justNumbers $x3
18006326223
assuming of course you have 11.4 (procs support), you can use this function or a similar one with string map (might be a little faster) and call it as necessary from same or different iRules.
yeah, the string map (with these particular mappings) is far faster than my solution:
% time { string map {"." "" " " "" "-" "" "(" "" ")" ""} $x3 } 100000
1.10809 microseconds per iteration
% time { justNumbers $x3 } 100000
9.95664 microseconds per iteration
Hey Mike, Congratulations! You are the very first in a new blog series on Q&A Highlights, where we’ll take an interesting question with a great solution and highlight it in an article. I have a DevCentral tshirt I’ll send you if you email me a shipping address and size. j dot rahm at f5 dot com. https://devcentral.f5.com/s/articles/qa-highlights-phone-number-normalization
Getting ready to test this in a few minutes, hopefully it will be the solution we need. ![]()
when ACCESS_POLICY_AGENT_EVENT { expr srand([clock clicks]) set otp [string range [format “%08d” [expr int(rand() * 1e9)]] 1 6 ] set mail [ACCESS::session data get “session.ldap.last.attr.mail”] set mobile [ACCESS::session data get [string map {“.” “” " " “” “-” “” “(” “” “)” “” “+” “”} “session.ldap.last.attr.mobile”]]
ACCESS::session data set session.user.otp.pw $otp ACCESS::session data set session.user.otp.mobile $mobile ACCESS::session data set session.user.otp.username [ACCESS::session data get “session.logon.last.username”] }
formatting got dorked up there ![]()
when ACCESS_POLICY_AGENT_EVENT {
expr srand([clock clicks])
set otp [string range [format "%08d" [expr int(rand() * 1e9)]] 1 6 ]
set mail [ACCESS::session data get "session.ldap.last.attr.mail"]
set mobile [ACCESS::session data get [string map {"." "" " " "" "-" "" "(" "" ")" "" "+" ""} "session.ldap.last.attr.mobile"]]
ACCESS::session data set session.user.otp.pw $otp
ACCESS::session data set session.user.otp.mobile $mobile
ACCESS::session data set session.user.otp.username [ACCESS::session data get "session.logon.last.username"]
}
I will update again after testing, a big Thank You to everyone to jumped in. ![]()
Well, that did not work unfortunately. The variable “session.user.otp.mobile” is blank when the process failed to complete. I guess I can’t do it all in one step maybe?
Try:
set mobile [string map {"." "" " " "" "-" "" "(" "" ")" "" "+" ""} [ACCESS::session data get session.ldap.last.attr.mobile]]
OK, two steps got it to work.
Code
when ACCESS_POLICY_AGENT_EVENT {
expr srand([clock clicks])
set otp [string range [format "%08d" [expr int(rand() * 1e9)]] 1 6 ]
set mail [ACCESS::session data get "session.ldap.last.attr.mail"]
set mobile [ACCESS::session data get "session.ldap.last.attr.mobile"]
set mobile2 [string map {"." "" " " "" "-" "" "(" "" ")" "" "+" ""} $mobile]
ACCESS::session data set session.user.otp.pw $otp
ACCESS::session data set session.user.otp.mobile $mobile2
ACCESS::session data set session.user.otp.username [ACCESS::session data get "session.logon.last.username"]
}
Hi Kevin,
That worked as well, thanks for throwing that out there as it makes me realize the order can make a big difference there.
Hi Jason, I was thinking about it once I got home looking in my closet, and I would like to ask if Kevin could get the shirt as he was a real highlight on how helpful the community can be. Thanks!
One more thing. Because you originally asked about a regex function:
% set num1 "1234567890"
% set num2 "123-456-7890"
% set num3 "123.456.7890"
% set num4 "123 456 7890"
% set num5 "(123) 456-7890"
% set num6 "123foo456bar7890"
% time { regsub -all {\D} $num1 "" newval; puts $newval }
1234567890
26 microseconds per iteration
% time { regsub -all {\D} $num2 "" newval; puts $newval }
1234567890
29 microseconds per iteration
% time { regsub -all {\D} $num3 "" newval; puts $newval }
1234567890
31 microseconds per iteration
% time { regsub -all {\D} $num4 "" newval; puts $newval }
1234567890
37 microseconds per iteration
% time { regsub -all {\D} $num5 "" newval; puts $newval }
1234567890
32 microseconds per iteration
% time { regsub -all {\D} $num6 "" newval; puts $newval }
1234567890
35 microseconds per iteration
% time { puts [string map {"." "" " " "" "-" "" "(" "" ")" "" "+" ""} $num3] }
1234567890
45 microseconds per iteration
The “\D” regex option used in regsub will replace ANY non-numeric character (see num6), so it’ll technically do a better job, and is more flexible than the string map version.
You can see the penalty you pay for using regex. If stripping all non-numeric is the goal, better off with the proc.
Indeed:
% set num1 "123-456-7890"
% time { regsub -all {\D} $num1 "" } 10000
3.172 microseconds per iteration
% time { string map {"." "" " " "" "-" "" "(" "" ")" ""} $num1 } 10000
1.1674 microseconds per iteration
So then it’s really a trade-off between speed and flexibility. Regex will catch more anomalies (ie. “123foo456bar7890”) at the expense of CPU time. That said, the goal here was convert a string during access policy evaluation, which should only happen once per session.