Forum Discussion
iRule regex to normalize phone numbers?
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: https://devcentral.f5.com/articles/one-time-passwords-via-an-sms-gateway-with-big-ip-access-policy-manager.UioetD99-jy 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. :)
16 Replies
- JRahm
Admin
Hey Mike, you might be able to cobble together what you're looking for from this Stack thread.
- Mike_Finney_119
Nimbostratus
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.
- Kevin_Stewart
Employee
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 - JRahm
Admin
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 18006326223assuming 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.
- JRahm
Admin
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 - JRahm
Admin
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 - Mike_Finney_119
Nimbostratus
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"] }
- Mike_Finney_119
Nimbostratus
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. 😄
- Mike_Finney_119
Nimbostratus
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?
- Kevin_Stewart
Employee
Try:
set mobile [string map {"." "" " " "" "-" "" "(" "" ")" "" "+" ""} [ACCESS::session data get session.ldap.last.attr.mobile]]
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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