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

Avraham Makeler amakeler at bezeqint.net
Wed Apr 21 18:37:30 EDT 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.







More information about the Python-list mailing list