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

J

Jeff Epler

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
 
F

Fredrik Lundh

Avraham said:
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>
 
A

Avraham Makeler

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top