⇤ ← Revision 1 as of 2004-12-12 04:30:56
740
Comment: search, match, split, and other notes
|
1352
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. |
PythonLibraryReference, [http://www.python.org/doc/current/lib/module-re.html 4.2 re module]
PythonLibraryReference, [http://www.python.org/doc/current/lib/re-syntax.html 4.2.1 Regular Expression Syntax]
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:
...which produces:
["This is a ", "((test))", " of the ", "((emergency broadcasting station.))" ]
Compiling
If you use a regex a lot, compile it first.
Consider:
...which outputs: ('h1', 'robot', 'h1')
If you were going to do that match a lot, you could compile it, like so:
...which yields the same result.
I don't know how much faster compiled forms are than non-compiled forms.