开发者

regular expression search in python

开发者 https://www.devze.com 2022-12-29 15:27 出处:网络
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

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 regula开发者_如何学Cr 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


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


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


What for?

print String.split()[4]


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


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.


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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号