[Tutor] Parse multi line with re module.
joel.goldstick at gmail.com
Tue Jan 3 10:21:48 EST 2012
On Tue, Jan 3, 2012 at 9:13 AM, Ganesh Kumar <bugcy013 at gmail.com> wrote:
> Hi Guys,
> I want parse multiple line. with re.module, this is my given string
> http://dpaste.com/680760/ I have try with re.compile module. I want parse
> two line mac address and channel,
> I have done with for mac address finding
> r = re.compile("^Searching for OPUSH on (\w\w(:\w\w)+)")
> for channel finding
> device_r = re.compile("^Channel: (\d+)")
> the two parsing string working. but I want combine two pattern in to one.
> This is my code
> please guide me .
> Did I learn something today? If not, I wasted it.
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
Do you know about str.startswith()
Since your data is so well defined I think it would be simpler to just
read each line, strip whitespace, use startswith() to check for
"Searching for OPUSH on" and "Channel:"
You can slice to pull out the mac address and the channel number like so:
>>> s = "Searching for OPUSH on 00:1D:FD:06:99:99"
'00:1D:FD:06:99:99' # this will work if no other text follows the mac address
'00:1D:FD:06:99:99' # this will work to just capture the mac address
For the Channel number, after stripping whitespace, take the slice from s[9:]
Searching for OPUSH on 00:1D:FD:06:99:99 ...
More information about the Python-list