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

Discussion in 'Python' started by Jeff Epler, Apr 21, 2004.

  1. Jeff Epler

    Jeff Epler Guest

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

    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
    Jeff Epler, Apr 21, 2004
    #1
    1. Advertising

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

    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>
    Fredrik Lundh, Apr 21, 2004
    #2
    1. Advertising

  3. 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.
    Avraham Makeler, Apr 21, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    792
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    924
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    602
    Bill Scherer
    Apr 20, 2004
  4. Aj

    decimal to string conv

    Aj, Feb 27, 2009, in forum: Python
    Replies:
    2
    Views:
    443
Loading...

Share This Page