Differences between revisions 1 and 2
Revision 1 as of 2004-12-12 04:30:56
Size: 740
Editor: dsl254-010-130
Comment: search, match, split, and other notes
Revision 2 as of 2004-12-27 08:57:38
Size: 1352
Editor: aaron
Comment: notes on compiling regexes
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
#pragma section-numbers off
Line 24: Line 26:

== Compiling ==

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

Consider:

{{{
#!python
import re
match_obj=re.match( "<(.*?)>(.*?)</(.*?)>", "<h1>robot</h1>" )
print mo.groups()
}}}

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

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

{{{
#!python
import re
match_re=re.compile( "<(.*?)>(.*?)</(.*?)>" )
match_obj=match_re.match( "<h1>robot</h1>" )
print mo.groups()
}}}

...which yields the same result.

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

You can search or match.

  • search -- find something anywhere in the string, and return it

  • match -- find something from the beginning of the string, and return it

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.

RegularExpression (last edited 2015-08-30 10:11:18 by pythonguru)

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