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

gcmartijn at gmail.com gcmartijn at gmail.com
Sat Jul 26 08:54:14 EDT 2008


On 26 jul, 14:25, Fredrik Lundh <fred... at pythonware.com> wrote:
> gcmart... 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>

Thanks for the info, I will download that program later so I can build
a re (i hope)

Because I can't wait for that re, I have made a non re solution what
is working for now.

for a in bla.split():
    if a.find('BlaObject')<>-1:
        print a[11:].replace("'","").replace('"',"").replace(",","")

(I know this is not the best way, but it helps me for now)



More information about the Python-list mailing list