Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > from newbie: how specify # of times to invoke a regex; conv tuple to string

Reply
Thread Tools

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

 
 
Jeff Epler
Guest
Posts: n/a
 
      04-21-2004
Instead of .findall(), you could use .finditer() and only consume a
few results. Otherwise, you can write your own code to use the 2-arg
version of .search() to start at the first position after the end of
the previous match.

If you want to take the tuple
t = ('e', '', '8', '.')
and get the string
s = 'e8.'
you want
s = ''.join(t)

Jeff

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      04-21-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>




 
Reply With Quote
 
 
 
 
Avraham Makeler
Guest
Posts: n/a
 
      04-21-2004

Hi all,

Question 1:

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.

Alternatively, realizing that reg exs are probably not supposed to be
used that way anyway, the responsibility for the number of executions should
probably rather be put on some method of the MyRE object. However, I could
not find a method that specifies the number of times that the reg ex search
should be performed on the input file.

Question 2:

Summary of Question: How do you convert a tuple to a string?

When I get the list of notes returned from the MyRE.findall()
function, it is returned as a list of tuples, where each musical note is
represented by one tuple. The tuple contains a set of strings where each
string is the result of the reg ex match per group.

For example: the raw input is matched and returned as the following
tuple:

('e', '', '8', '.')

I could not find a function that converts a tuple to a string. Please
tell me where it is and in which module it is. Yes, I can loop through the
tuples, and then loop through the string components of the tuple to build
the result string, but isn't there a single function that does it?

Thanks.

Avraham Makeler.




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
decimal to string conv Aj Python 2 02-27-2009 10:07 AM
Easily convert unicode tuple to python string tuple??? Michal Mikolajczyk Python 1 04-20-2004 08:37 PM
Re: Easily convert unicode tuple to python string tuple??? Jeff Epler Python 0 04-20-2004 03:36 PM
Re: Easily convert unicode tuple to python string tuple??? Bill Scherer Python 0 04-20-2004 03:34 PM



Advertisments