re.findall(a patern,'function(dsf sdf sdf)')

Fredrik Lundh fredrik at pythonware.com
Sat Jul 26 08:25:49 EDT 2008


gcmartijn at gmail.com wrote:

> - For me its hard to learn the re , I will try to search again at
> google for examples and do some copy past things.

this might be useful when figuring out how RE:s work:

     http://kodos.sourceforge.net/

also, don't forget the following guideline:

    "Some people, when confronted with a problem, think 'I know,
    I'll use regular expressions.'   Now they have two problems."

some advice:

- Keep the RE:s simple.  You can often simplify things a lot by doing 
multiple searches, or even by applying a second RE on the results from 
the first.  In this case, you could use one RE to search for BlaObject, 
and then use another one to extract the first argument.

- Ordinary string methods (e.g. find, partition, split) are often a very 
capable alternative (in combination with simple RE:s).  In your case, 
for JavaScript code that's as regular as the one in your example, you 
can split the string on "BlaObject(" and then use partition to strip off 
the first argument.

- only use RE:s to read specialized file formats if you know exactly 
what you're doing; there's often a ready-made library that does it much 
better.

- The regular expression machinery is not a parser.  You cannot handle 
all possible syntaxes with it, so don't even try.

</F>




More information about the Python-list mailing list