lua-users home
lua-l archive

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


* On 2013-11-16 19:42:36 +0100, Laurent FAILLIE wrote:
 
> 2 questions related to the same kind of strings :
> 
> 1/ I have a string like this one :
> l="Débit ATM              3939 kb/s          945 kb/s"
> 
> How can I extract only numbers ?

Make sure the pattern expects the characters between the numbers:

l:match("(%d+).-(%d+)")

> 2/ If I want to generalize, I got a set of strings link
> 
>   Débit ATM              3939 kb/s          945 kb/s          
>   Marge de bruit         8.10 dB            8.20 dB           
>   Atténuation            56.00 dB           33.40 dB          
>   FEC                    21976              196               
>   CRC                    600                0                 
>   HEC                    38                 184               
> 
> I would like to retrieve :
> lab : the line label ('Débit ATM' or 'Marge de bruit', ...)
> val1 : first value
> val2 : second value.

Assuming there is always two or more spaces or tabs between the labels
and the rest of the line:

label, val1, val2 = l:match("(.-)%s%s+([%d%.]+).-([%d%.]+)")

(.-)       Match as few characters as possible ...
%s%s+      ... up to two or more spaces
([%d%.]+)  Match a sequence of digits and dots ...
.-         ignore stuff in between ...
([%d%.]+)  ... and again match a sequence of digits and dots

Beware: in your original mail, as received by my MUA, the white space
between the labels and the numbers are not encoded by spaces or tabs,
but by ascii 0xa0 (non breaking space), which is not matched by the %s
in the patterns.

This might be caused by the transit by mail, but if your original data
contains nbsp's as well, the above pattern will fail.

-- 
:wq
^X^Cy^K^X^C^C^C^C