Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18417 > unrolled thread

Re: [Tutor] Parse multi line with re module.

Started byJoel Goldstick <joel.goldstick@gmail.com>
First post2012-01-03 10:21 -0500
Last post2012-01-03 10:21 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: [Tutor] Parse multi line with re module. Joel Goldstick <joel.goldstick@gmail.com> - 2012-01-03 10:21 -0500

#18417 — Re: [Tutor] Parse multi line with re module.

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2012-01-03 10:21 -0500
SubjectRe: [Tutor] Parse multi line with re module.
Message-ID<mailman.4356.1325604112.27778.python-list@python.org>
On Tue, Jan 3, 2012 at 9:13 AM, Ganesh Kumar <bugcy013@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
> http://www.bpaste.net/show/21323/
>
> please guide me .
>
>
> -Ganesh
>
> Did I learn something today? If not, I wasted it.
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 


Do you know about str.startswith()
http://docs.python.org/library/stdtypes.html#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"
>>> s[23:]
'00:1D:FD:06:99:99'   # this will work if no other text follows the mac address
>>> s[23:41]
'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 ...
    Channel: 9

Joel Goldstick

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web