Lost (again) with strings patterns

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Lost (again) with strings patterns

Laurent FAILLIE
Hello,

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 ?
If I do
    print( l:match('(%d+)(%d+)' ))
I got only :
    393    9

'9' being the last digit of 3939.

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.

I played a long time with patterns without luck, so ...

Thanks

Laurent
Reply | Threaded
Open this post in threaded view
|

Re: Lost (again) with strings patterns

steve donovan
On Sat, Nov 16, 2013 at 8:42 PM, Laurent FAILLIE <[hidden email]> wrote:
> l="Débit ATM              3939 kb/s          945 kb/s"
>     print( l:match('(%d+)(%d+)' ))
> I got only :
>     393    9

Yes, Lua string patterns are not that intelligent!  You need to say
that the two integers are separated by some stuff:

'(%d)%s+kb/s%s+(%d)'

I'm using %s instead of plain ' ' because you might have tabs or some
such there, and doing patterns like this makes them more flexible.

Reply | Threaded
Open this post in threaded view
|

Re: Lost (again) with strings patterns

steve donovan
On Sat, Nov 16, 2013 at 8:51 PM, steve donovan
<[hidden email]> wrote:
> '(%d)%s+kb/s%s+(%d)'

s/%d/%d+/g

Reply | Threaded
Open this post in threaded view
|

Re: Lost (again) with strings patterns

Choonster TheMage
In reply to this post by Laurent FAILLIE
On 17 November 2013 05:42, Laurent FAILLIE <[hidden email]> wrote:

> 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.
>
> I played a long time with patterns without luck, so ...
>
> Thanks
>
> Laurent

local lab, val1, val2 = str:match("^(.-)%s%s+(%d+).-(%d+)")

Reply | Threaded
Open this post in threaded view
|

Re: Lost (again) with strings patterns

Ico Doornekamp
In reply to this post by Laurent FAILLIE
* 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

Reply | Threaded
Open this post in threaded view
|

Re: Lost (again) with strings patterns

Petite Abeille
In reply to this post by Laurent FAILLIE

On Nov 16, 2013, at 7:42 PM, Laurent FAILLIE <[hidden email]> wrote:

> 2/ If I want to generalize, I got a set of strings link

Aren’t your strings fix length?

E.g.:

aLabel = aLine:sub( 1, 23 )
aFirstValue = aLine:sub( 24, 42 )
aSecondValue = aLine:sub( 43 )


Reply | Threaded
Open this post in threaded view
|

Re: Lost (again) with strings patterns

Laurent FAILLIE
Thanks you all for your detailed explanation :)

So I choose :
    l:match("^%s*(.-)%s+(%d+).-(%d+)")

Petite Abeille > Aren’t your strings fix length?
Yes ... but as my data source come from my ISP, I'm not sure I can rely on that. I mean, it may be trashed with next firmware update.

Thanks again.

---
The misspelling master is on the Web.
  _________ 100 % Dictionnary Free !
/               /(
/ Dico       / / Pleins d'autres fautes sur
/________/ /
(#######( / http://destroyedlolo.info
Quoi, des fautes d'orthographe! Pas possible ;-D.


On Saturday, November 16, 2013 8:38 PM, Petite Abeille <[hidden email]> wrote:

On Nov 16, 2013, at 7:42 PM, Laurent FAILLIE <[hidden email]> wrote:

> 2/ If I want to generalize, I got a set of strings link


Aren’t your strings fix length?

E.g.:

aLabel = aLine:sub( 1, 23 )
aFirstValue = aLine:sub( 24, 42 )
aSecondValue = aLine:sub( 43 )