david g
david g

Reputation: 1

Regular expression to match second or last decimal number in a string

String:

<LF><CR>A214 pH/ISE,X00066,2.59,ABCDE,10/16/13 22:06:59,ABC1,CH-1,pH,7.00,pH,0.0, mV,25.0,C,100.0,%,M100,#35<LF><CR>

I need to match only the 7.00 - This number could be anywhere from 0.00 - 14.00 (its a pH reading).

Right now I can only come up with [0-9]{1,2}\.[0-9]{2} which also matches the software revision number which appears earlier in the string (2.59)

Any help is greatly appreciated.

EDIT: Thanks everyone. I figured it out by using [0-9]{1,2}\.[0-9]{2}(?=,p)

Upvotes: 0

Views: 247

Answers (3)

Fredrik Pihl
Fredrik Pihl

Reputation: 45662

If the format of the string is fixed, i.e. the data is in the 9th position if you split on , Use e.g. awk:

$ awk -F, '{print $8, $9}' input
pH 7.00

or using perl in awk-mode:

$ perl -F, -lane 'print $F[8]' input
7.00

Or this regexp

pH,(\d+\.\d{2})

See it line on http://www.rubular.com/r/3kkWNVBAi8

Upvotes: 0

Sidorov Andrew
Sidorov Andrew

Reputation: 11

maybe you can use that:

pH,(([0-9]|1[0-4])\.\d{2}),pH

group 1 match number that you need. And that control data

Upvotes: 0

utdemir
utdemir

Reputation: 27216

Simply find all entries and get the last:

>>> s = "A214 pH/ISE,X00066,2.59,ABCDE,10/16/13 22:06:59,ABC1,CH-1,pH,7.00,pH,0.0, mV,25.0,C,100.0,%,M100,#35"
>>> re.findall("[0-9]{1,2}.[0-9]{2}", s)[-1]
'7.00'

You can improve that regex by using the information that PH is between 0-14(first digit can only by one etc). Or better, just split by commas or use csv module.

Upvotes: 1

Related Questions