from newbie: how specify # of times to invoke a regex; conv tuple to string

Fredrik Lundh fredrik at pythonware.com
Wed Apr 21 12:03:40 EDT 2004


Avraham Makeler wrote:

>       Summary of Question: How do you specify the number of times to run a
> reg ex search using a complex reg ex?
>
>        The reg _expression I wrote goes like this:
>
>                    MyRE =
> re.compile('([k-z])(?:\'{6,9})(64|32|16|8|4|2|1)(\.)?')
>
>        I ran it using this:
>
>                    tplstFound = MyRE.findall(InputStr)
>
>       The problem is that this solution processes the whole input string. I
> only need the first matches notes. (I don't know if this is going to make
> any practical difference, since the name generation is a one-time thing, and
> anyway PCs are very fast these days.)
>
>       I could not see a way of writing the reg ex so that it specifies only
> the first 25 matches. I tried putting the {m,n} construct (ie {0,25}) at the
> end of the re ex but it did not work.

{m,n} binds to the last expression element, not the entire expression.
use (?:re){m,n} to repeat the entire re:

    "(?:([k-z])(?:\'{6,9})(64|32|16|8|4|2|1)(\.)?){0,25}"

</F>







More information about the Python-list mailing list