[Tutor] regex question

Brett Ritter swiftone at swiftone.org
Tue Jan 4 18:31:07 CET 2011


On Tue, Jan 4, 2011 at 10:37 AM, Richard D. Moores <rdmoores at gmail.com> wrote:
> regex = ".*" + search + ".*"
> p = re.compile(regex, re.I)
>
> in finding lines in a text file that contain search, a string entered
> at a prompt.

That's an inefficient regex (though the compiler may be smart enough
to prune the unneeded ".*").

Just having "search" as your regex is fine (it will search for the
pattern _in_ the string, no need to specify the other parts of the
string), but if you're not using any special regex characters you're
probably better off not using a regex and just using a string
operation.

Regexes are great for trying to do powerful and complicated things -
and as such may be too complicated if you're trying to do a simple
thing.

-- 
Brett Ritter / SwiftOne
swiftone at swiftone.org


More information about the Tutor mailing list