lua-users home
lua-l archive

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


Pete Kay wrote:
> Hi,
> I am write a price of code in Lua to break down phone number to country
> code, area code, and local code
> For instance, 16502839332 -> 1      650    2839332
> I have a series of country code, area code that I can match against. 
> Previously with another language, I would do a comparision against each
> code to breakdown the number.  With Lua, is there a more efficient way
> of doing this?
>  
> I checked the String function in wiki and can't see anything.  I know I
> can do a find and then sub to retrieve each code one by one, but I hope
> that there is a better way to do it.
>  
> Any suggestion will be greatly appreciated.

For string patterns, it's best to use regular expressions. Regexes
will save you from writing enormous amounts of code.

For a line like the above, you can use something like (untested):
... string.match(x, "^(%d+)%s+%->%s+(%d+)%s+(%d+)%s+(%d+)$")

Please see the reference manual for more information.

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