regex - Parsing message parameters received by a GSM modem in python -


i'm trying parse messages receive gsm modem in python.

i have lot of messages need parse. receive new messages every couple of hours or so.

here's example of data receive after reading data modem using serial object list x.

at+cmgl="all"   +cmgl: 1,"rec read","+918884100421","","13/04/05,08:24:36+22" here's message 1   +cmgl: 2,"rec read","+918884100421","","13/04/05,09:40:38+22" here's message 2  +cmgl: 3,"rec read","+918884100421","","13/04/05,09:41:04+22" here's message 3  +cmgl: 4,"rec read","+918884100421","","13/04/05,10:04:18+22" here's message 4  +cmgl: 5,"rec read","+918884100421","","13/04/05,10:04:32+22" here's message 5  . . . . . 

there lot more messages, i've listed 5 here.

my main intention extract content of message, example "here's message one" , on every message receive.

here's code i'm using right now.

def reading():      print "reading messages stored on sim card"      phone.write(b'at+cmgl="all"\r')      sleeps()      x=phone.read(10000)      sleeps()      print x      print "now parsing message!"      k="".join(x)      parse(k)      k="" def parse(k):     m = re.search("\+cmgl: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\r\n(.+)\r\n",k)     print "6="     print m.group(6) 

phone serial object i'm using read gsm modem.

here m.group(6) captures message content of first message "here's message one"

how can match content of messages, not first one.

i tried setting multiline flag didn't work. neither did using re.findall() instead of re.search().

also match object returned re.search isn't iterable.

please help.

since don't material make sample.

'\xef\xbb\xbfat+cmgl="all"\n\n+cmgl: 1,"rec read","+918884100421","","13/04/05,08:24:36+22"\nhere\'s message 1 \n\n+cmgl: 2,"rec read","+918884100421","","13/04/05,09:40:38+22"\nhere\'s message two\n\n+cmgl: 3,"rec read","+918884100421","","13/04/05,09:41:04+22"\nhere\'s message three\n\n+cmgl: 4,"rec read","+918884100421","","13/04/05,10:04:18+22"\nhere\'s message four\n\n+cmgl: 5,"rec read","+918884100421","","13/04/05,10:04:32+22"\nhere\'s message five\n' 

this comes question using ''.join(). , use regex pattern, replace \r\n \n because sample use using \n. , result. don't know why findall doesn't work you.

def parse(x):     res = []     match = re.finditer("\+cmgl: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\n(.+)\n", x)     each in match:         res.append(each.group(6))     return res 

the result ["here's message 1 ", "here's message two", "here's message three", "here's message four", "here's message five"]. finditer returns iterator , findall works ok.

 def parse(x):         res = []         match = re.findall("\+cmgl: (\d+),""(.+)"",""(.+)"",(.*),""(.+)""\n(.+)\n", x)         each in match:             res.append(each[5])         return res 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -