Revision 2 as of 2004-12-27 08:57:38

Clear message

You can search or match.

You can also split on a pattern.

For example:

   1 import re
   2 split_up = re.split( r"(\(\([^)]+\)\))",
   3                      "This is a ((test)) of the ((emergency broadcasting station.))" )

...which produces:

["This is a ", "((test))", " of the ", "((emergency broadcasting station.))" ]

Compiling

If you use a regex a lot, compile it first.

Consider:

   1 import re
   2 match_obj=re.match( "<(.*?)>(.*?)</(.*?)>", "<h1>robot</h1>" )
   3 print mo.groups()

...which outputs: ('h1', 'robot', 'h1')

If you were going to do that match a lot, you could compile it, like so:

   1 import re
   2 match_re=re.compile( "<(.*?)>(.*?)</(.*?)>" )
   3 match_obj=match_re.match( "<h1>robot</h1>" )
   4 print mo.groups()

...which yields the same result.

I don't know how much faster compiled forms are than non-compiled forms.

Unable to edit the page? See the FrontPage for instructions.