[BangPypers] Parse multi line with re module.

Gora Mohanty gora at mimirtech.com
Tue Jan 3 15:33:53 CET 2012

On Tue, Jan 3, 2012 at 7:23 PM, 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.

There are several issues here:
* Your regex for a MAC address is not quite right. From what I remember,
   a MAC address is six groups of two hexadecimal digits, separated by ':'
  or '-'. Thus, you need something like ([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2})
* This might depend on the Python version, but at least from 2.6.5 onwards,
   one can specify re.DOTALL as a regex compilation flag that makes '.' match
   any character, including a newline.

Thus, following your example:
r = re.compile( '.*([0-9a-fA-F]{2}[:-]){5}([0-9a-fA-F]{2}).*Channel:
(\d+).*', re.DOTALL )
s = '''
Inquiring ...
Searching for OPUSH on 00:1D:FD:06:99:99 ...
Service Name: OBEX Object Push
Service RecHandle: 0x10135
Service Class ID List:
  "OBEX Object Push" (0x1105)
Protocol Descriptor List:
  "L2CAP" (0x0100)
  "RFCOMM" (0x0003)
    Channel: 9
  "OBEX" (0x0008)
Language Base Attr List:
  code_ISO639: 0x454e
  encoding:    0x6a
  base_offset: 0x100
Profile Descriptor List:
  "OBEX Object Push" (0x1105)
    Version: 0x0100
re.match( s )
should give you a _sre.SRE_Match object


More information about the BangPypers mailing list