views:

99

answers:

6

Hello all,

I am trying to parse some data and just started reading up on regular Expressions so I am pretty new to it. This is the code I have so far

String = "MEASUREMENT   3835    303 Oxygen:     235.78 Saturation:      90.51 Temperature:      24.41 DPhase:      33.07 BPhase:      29.56 RPhase:       0.00 BAmp:     368.57 BPot:      18.00 RAmp:       0.00 RawTem.:           68.21"
String = String.strip('\t\x11\x13')

String = String.split("Oxygen:")
print String[1]
String[1].lstrip
print String[1]

What I am trying to do is to do is remove the oxygen data (235.78) and put it in its own variable using an regular expression search. I realize that there should be an easy solution but I am trying to figure out how regular expressions work and they are making my head hurt. Thanks for any help

Richard

A: 

What for?

print String.split()[4]
Ignacio Vazquez-Abrams
To make sure that I am not getting garbage for data. If there is no match I can deal with it some other way
Richard
So then make sure that element `[3]` equals `Oxygen:`.
Ignacio Vazquez-Abrams
+2  A: 
re.search( r"Oxygen: *([\d.]+)", String ).group( 1 )
jchl
nit-pick: `r'Oxygen:\s*(\d+(\.\d+)?)'` is more likely to hit on unexpected input as it doesn't require space between 'Oxygen:' and the value and will accept integers as well reals. "Be liberal in what you accept" -Jon Postel
msw
A: 
import re
string = "blabla Oxygen:      10.10 blabla"
regex_oxygen = re.compile('''Oxygen:\W+([0-9.]*)''')
result = re.findall(regex_oxygen,string)
print result
Robus
A: 

For general parsing of lists like this one could

import re
String = "MEASUREMENT   3835    303 Oxygen:     235.78 Saturation:      90.51"
String = String.replace(':','')
value_list=re.split("MEASUREMENT\W+[0-9]+\W+[0-9]+\W",String)[1].rstrip().split()
values = dict(zip(value_list[::2],map(float,value_list[1::2])))
pixelbeat
A: 

I would like to share my ?is this an email? regex expresion, just to inspire you. :)

  9 emailregex = "^[a-zA-Z.a-zA-Z][email protected]$"
 10
 11 def validateEmail(email):
 12         """returns 1 if is an email, 0 if not """
 13         # len([email protected]) = 17
 14         if len(email)>=17:
 15                 if re.match(emailregex,email)!= None:
 16                         return 1
 17         return 0
permalac
A: 

I believe the answer to you specific problem has been posted. However I wanted to show you a few ressource for regular expression for python. The python documentation on regular expression is the place to start.

O'reilly also has many good books on the subject, either if you want to understand regular expression deep down or just enough to make things work.

Finally regular-expressions.info is a good ressource for regular expression among mainstream languages. You can even test your regular expression on the website.

afilatun