lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Pete Kay wrote:
> But the part that is complicated is that area code can have different
> number of digits, such as 1 or 851 or 852
> Based on the country code, area code can be 2 -3 digits long.
> For instance, if the country code is 852, there is no area code.  If the
> country code is 1, then area code is 3 digits.

Sorry about my first post, lack of caffeine. :-)

Looking at:
http://en.wikipedia.org/wiki/List_of_country_calling_codes
http://en.wikipedia.org/wiki/List_of_NANP_area_codes

> [snip] 
> For regex, I think it assumes a certain number of digits for each part. 
> If the number of digits is variable but is contained in a "set", is it
> possible to do it efficiently in lua?

It looks like we can't escape a string.sub. For most countries,
all we need is one lookup table to recognize valid country codes.

Read successively longer prefixes until we get a hit for the
lookup table. The table key will be the valid country code. If the
prefix is '1', then we'll need an additional table for NANP area
codes. But if we don't want to validate, perhaps we can just grab
the next 3 digits as area code and take it on faith.

-- 
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia