itsmedoofer
Posts: 356
Joined: Wed Sep 25, 2013 8:43 am

regex question

Thu Jul 11, 2019 3:55 pm

Hi,

I want to pull some values out of iwconfig wlan0, for example

Link Quality=68/70

The first figure is up the second down

If I use (\d*\/?\d*) I can pull both figures, this is what have got at the moment my result is displayed 68/70

If I use (\d*) I can get the first figure

How on earth do I get the second (70) ?

Thanks in advance.

jahboater
Posts: 4604
Joined: Wed Feb 04, 2015 6:38 pm

Re: regex question

Thu Jul 11, 2019 4:00 pm

itsmedoofer wrote:
Thu Jul 11, 2019 3:55 pm
Hi,

I want to pull some values out of iwconfig wlan0, for example

Link Quality=68/70

The first figure is up the second down

If I use (\d*\/?\d*) I can pull both figures, this is what have got at the moment my result is displayed 68/70

If I use (\d*) I can get the first figure

How on earth do I get the second (70) ?
You may concatenate expressions

(\d*)/(\d*)

assuming the / will always be present. If the / may be missing as in 6870 you can split the the numbers with a range (\d\{2\})(\d\{2\}) or some such - I don't know what type of RE's you are using. That is match exactly two digits.

itsmedoofer
Posts: 356
Joined: Wed Sep 25, 2013 8:43 am

Re: regex question

Thu Jul 11, 2019 4:12 pm

Hi ,

Thanks for getting back to me so quickly, really appreciated.

I didn't express myself very clearly, sorry !

So at the moment my data is presented as

Link Quality 68/70

I want to break this up so it is presented

Link Quality up 68
Link Quality down 70

so for one regex I pull the first value, 68 and then for the other I pull the second, 70...

Not really messed about with these before, I'm in over my head lol. I'm adding more stats to RPI-Monitor

jahboater
Posts: 4604
Joined: Wed Feb 04, 2015 6:38 pm

Re: regex question

Thu Jul 11, 2019 4:14 pm

How are you doing this? sed ?

Can you just replace the '/' with "\nLink Quality down " somehow.

itsmedoofer
Posts: 356
Joined: Wed Sep 25, 2013 8:43 am

Re: regex question

Thu Jul 11, 2019 4:21 pm

It's a config parameter in RPI-Monitor...

https://github.com/XavierBerger/RPi-Monitor

I don't know what does on under the hood...

So this is what I have created for the 68/70

Code: Select all

dynamic.6.name=wifi_linkqual
dynamic.6.source=iwconfig wlan0 | grep Link
dynamic.6.regexp=Link Quality=(\d*\/?\d*)
dynamic.6.postprocess=
dynamic.6.rrd=GAUGE
What I was hoping to do was

Code: Select all

dynamic.6.name=wifi_linkqualup
dynamic.6.source=iwconfig wlan0 | grep Link
dynamic.6.regexp=Link Quality=(\d*)
dynamic.6.postprocess=
dynamic.6.rrd=GAUGE

dynamic.7.name=wifi_linkqualdw
dynamic.7.source=iwconfig wlan0 | grep Link
dynamic.7.regexp=Link Quality=(\d*\/?\d*) <HELP
dynamic.7.postprocess=
dynamic.7.rrd=GAUGE

Silverlock
Posts: 87
Joined: Wed Apr 10, 2013 3:51 am

Re: regex question

Thu Jul 11, 2019 4:49 pm

Try this:

Code: Select all

dynamic.6.name=wifi_linkqualup
dynamic.6.source=iwconfig wlan0 | grep Link
dynamic.6.regexp=Link Quality=(\d*)/?\d*
dynamic.6.postprocess=
dynamic.6.rrd=GAUGE

dynamic.7.name=wifi_linkqualdw
dynamic.7.source=iwconfig wlan0 | grep Link
dynamic.7.regexp=Link Quality=\d*\/?(\d*)
dynamic.7.postprocess=
dynamic.7.rrd=GAUGE
RPi3+external HDD/Raspbian Stretch

itsmedoofer
Posts: 356
Joined: Wed Sep 25, 2013 8:43 am

Re: regex question

Thu Jul 11, 2019 4:54 pm

Silverlock wrote:
Thu Jul 11, 2019 4:49 pm
Try this:
Perfect !!! You are a hero, thanks so much really appreciated !!

User avatar
Paeryn
Posts: 2636
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: regex question

Thu Jul 11, 2019 5:37 pm

Hmm. The forum didn't notify me that another post had appeared in the time of me reading and replying...
itsmedoofer wrote:
Thu Jul 11, 2019 4:21 pm
So this is what I have created for the 68/70

Code: Select all

dynamic.6.name=wifi_linkqual
dynamic.6.source=iwconfig wlan0 | grep Link
dynamic.6.regexp=Link Quality=(\d*\/?\d*)
dynamic.6.postprocess=
dynamic.6.rrd=GAUGE
What I was hoping to do was

Code: Select all

dynamic.6.name=wifi_linkqualup
dynamic.6.source=iwconfig wlan0 | grep Link
dynamic.6.regexp=Link Quality=(\d*)
dynamic.6.postprocess=
dynamic.6.rrd=GAUGE

dynamic.7.name=wifi_linkqualdw
dynamic.7.source=iwconfig wlan0 | grep Link
dynamic.7.regexp=Link Quality=(\d*\/?\d*) <HELP
dynamic.7.postprocess=
dynamic.7.rrd=GAUGE
If it is getting the value just from the group i.e. what is matched inside the brackets () and using the rest of the regexp to find it then one of these, I'm guessing it uses POSIX Extended regexps since you used ?. I prefer to use character classes e.g. [:digit:] rather than escapes like \d. Also changed the * to + as I assume you want to match there being two numbers separated by a slash (your example could match any of 10/10, /10, 10, 10/, / or even an empty string.

Code: Select all

# Pick just the second number
dynamic.7.regexp=Link Quality=[[:digit:]]+\/([[:digit:]]+)
# Same as above but using \d instead of [[:digit:]]
dynamic.7.regexp=Link Quality=\d+\/(\d+)
She who travels light — forgot something.

jahboater
Posts: 4604
Joined: Wed Feb 04, 2015 6:38 pm

Re: regex question

Thu Jul 11, 2019 5:43 pm

Paeryn wrote:
Thu Jul 11, 2019 5:37 pm
I prefer to use character classes e.g. [:digit:] rather than escapes like \d.
I was just wondering why?
Is it because it makes scripts more readable?
I find \d so much quicker to type when I am searching for something in an editor.

User avatar
Paeryn
Posts: 2636
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: regex question

Thu Jul 11, 2019 8:53 pm

jahboater wrote:
Thu Jul 11, 2019 5:43 pm
Paeryn wrote:
Thu Jul 11, 2019 5:37 pm
I prefer to use character classes e.g. [:digit:] rather than escapes like \d.
I was just wondering why?
Is it because it makes scripts more readable?
I find \d so much quicker to type when I am searching for something in an editor.
I find it easier to read, I rarely need to write regexes so the couple of seconds extra it takes me to type the class is irrelevant. Also, although Vim has escape codes for most of the classes, Emacs' escape codes are different, by always using the class names I know it works.
She who travels light — forgot something.

Return to “General programming discussion”