Richard
Richard

Reputation: 15862

regular expression search in python

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

Upvotes: 1

Views: 530

Answers (6)

tmoisan
tmoisan

Reputation: 1221

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.

Upvotes: 0

Marc Riera
Marc Riera

Reputation: 301

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

Upvotes: -1

pixelbeat
pixelbeat

Reputation: 31708

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])))

Upvotes: 0

Robus
Robus

Reputation: 8259

import re
string = "blabla Oxygen:      10.10 blabla"
regex_oxygen = re.compile('''Oxygen:\W+([0-9.]*)''')
result = re.findall(regex_oxygen,string)
print result

Upvotes: 1

jchl
jchl

Reputation: 6522

re.search( r"Oxygen: *([\d.]+)", String ).group( 1 )

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

What for?

print String.split()[4]

Upvotes: 0

Related Questions