20120404 a better telephonenumber regex filter - plembo/onemoretech GitHub Wiki

title: A better telephonenumber regex filter link: https://onemoretech.wordpress.com/2012/04/04/a-better-telephonenumber-regex-filter/ author: lembobro description: post_id: 2481 created: 2012/04/04 17:00:49 created_gmt: 2012/04/04 21:00:49 comment_status: closed post_name: a-better-telephonenumber-regex-filter status: publish post_type: post

A better telephonenumber regex filter

This is an improvement on an old post. After getting some particularly weird data I was forced to rework my original code and come up with something new. Let me begin by admitting I don't know what exactly was wrong with the original data that came across. All I know is that it caused gedit to blow up and clearly contained some weird stuff that no telephone number should have. Oh yeah, this is phone number data. From Southern Europe, which in my world includes every country on the Mediterranean coast -- and then some. Here's the regex I used to use:

s/[^x20-x7Ex0Ax0D]//g;

The problem with this was that it allowed too many "non-telephone" characters, like colons (a red flag when you're dealing with Unicode) and all those pesky roman letters (you know, A-z) that people like to annotate their phone number data with (for example "Fax: +33 0 00 ...", you get the idea). Here's the improved version I came up with after learning more about the ASCII Character Table:

s/[^x20-x39]//g;

Basically what this does is filter out every character that is not in the range hex 20 to hex 39 ("Space" to the numeral 9). So all the pluses ("+") and infinite varieties of white space placements are allowed, along with actual numbers. But none of the awful stuff that makes feed parsers and character translation tools (like iconv) choke. END;

Copyright 2004-2019 Phil Lembo