OT: regular expression matching multiple occurrences of one group

Discussion in 'Python' started by pinkisntwell, Nov 9, 2009.

  1. pinkisntwell

    pinkisntwell Guest

    How can I make a regular expression that will match every occurrence
    of a group and return each occurrence as a group match? For example,
    for a string "-c-c-c-c-c", how can I make a regex which will return a
    group match for each occurrence of "-c"?
    pinkisntwell, Nov 9, 2009
    #1
    1. Advertising

  2. pinkisntwell schrieb:
    > How can I make a regular expression that will match every occurrence
    > of a group and return each occurrence as a group match? For example,
    > for a string "-c-c-c-c-c", how can I make a regex which will return a
    > group match for each occurrence of "-c"?


    Why is this flagged "OT"?

    And in python, you can't do that. Groups are based upon the lexical
    structure of the regexp, and thus have a fixed number of groups.

    Either use repetitive searches over the input, or postprocess the
    combined group by e.g. splitting it.

    Diez
    Diez B. Roggisch, Nov 9, 2009
    #2
    1. Advertising

  3. pinkisntwell

    Jon Clements Guest

    On Nov 9, 1:53 pm, pinkisntwell <> wrote:
    > How can I make a regular expression that will match every occurrence
    > of a group and return each occurrence as a group match? For example,
    > for a string "-c-c-c-c-c", how can I make a regex which will return a
    > group match for each occurrence of "-c"?


    As well as what Diez has said, unless you absolutely want regexp's,
    and by the sounds of it I'm guessing you may be after something more
    flexible: what about the pyparsing module [1]. It handles your
    situation quite nicely.

    >>> import pyparsing
    >>> parser = pyparsing.ZeroOrMore('-c')
    >>> parser.parseString('-c-c-c-c-c-c')

    (['-c', '-c', '-c', '-c', '-c', '-c'], {})

    hth,
    Jon.


    [1] http://pyparsing.wikispaces.com/
    Jon Clements, Nov 9, 2009
    #3
  4. pinkisntwell

    Guest

    On Mon, 9 Nov 2009 05:53:00 -0800 (PST), pinkisntwell <> wrote:

    >How can I make a regular expression that will match every occurrence
    >of a group and return each occurrence as a group match? For example,
    >for a string "-c-c-c-c-c", how can I make a regex which will return a
    >group match for each occurrence of "-c"?


    Is this a ludicrous question, or is it meant for the python group?

    use strict;
    use warnings;

    my @matches = "-c-c-c-c-c" =~ /(-c)/g;
    print "\n@matches\n";

    @matches = ();
    "-c-a-c-c-c" =~ /(?:(-c)(?{push @matches, $^N})|.)+/x;
    print "@matches\n";

    -sln
    , Nov 9, 2009
    #4
  5. pinkisntwell <> wrote:
    >How can I make a regular expression that will match every occurrence
    >of a group and return each occurrence as a group match? For example,
    >for a string "-c-c-c-c-c", how can I make a regex which will return a
    >group match for each occurrence of "-c"?


    Where is the problem? The most straight-forward, simplest approach works
    just fine:
    use strict; use warnings;
    my $s = '-c-c-c-c-c';

    my @matched = $s=~/-c/g;

    print "Found ". @matched . " occurences of '-c':\n";
    print join "\n", @matched;

    Or did you forget to use the g modifier?

    jue
    Jürgen Exner, Nov 9, 2009
    #5
  6. En Mon, 09 Nov 2009 12:59:53 -0300, Jon Clements <>
    escribió:
    > On Nov 9, 1:53 pm, pinkisntwell <> wrote:


    >> How can I make a regular expression that will match every occurrence
    >> of a group and return each occurrence as a group match? For example,
    >> for a string "-c-c-c-c-c", how can I make a regex which will return a
    >> group match for each occurrence of "-c"?

    >
    > As well as what Diez has said, unless you absolutely want regexp's,
    > and by the sounds of it I'm guessing you may be after something more
    > flexible: what about the pyparsing module [1]. It handles your
    > situation quite nicely.
    >
    >>>> import pyparsing
    >>>> parser = pyparsing.ZeroOrMore('-c')
    >>>> parser.parseString('-c-c-c-c-c-c')

    > (['-c', '-c', '-c', '-c', '-c', '-c'], {})


    Not that I like regexes very much, but findall does exactly that:

    py> re.findall('-c', '-c-c-c-c-c-c')
    ['-c', '-c', '-c', '-c', '-c', '-c']

    Now, the OP said "return each occurrence as a group match":

    py> for g in re.finditer("-c", '-c-c-c-c-c-c'): print g, g.span(),
    g.group()
    ....
    <_sre.SRE_Match object at 0x00C096E8> (0, 2) -c
    <_sre.SRE_Match object at 0x00C09410> (2, 4) -c
    <_sre.SRE_Match object at 0x00C096E8> (4, 6) -c
    <_sre.SRE_Match object at 0x00C09410> (6, 8) -c
    <_sre.SRE_Match object at 0x00C096E8> (8, 10) -c
    <_sre.SRE_Match object at 0x00C09410> (10, 12) -c

    --
    Gabriel Genellina
    Gabriel Genellina, Nov 10, 2009
    #6
    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. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    822
    Alan Moore
    Dec 2, 2005
  2. Replies:
    7
    Views:
    300
    Larry Bates
    Feb 24, 2006
  3. Replies:
    10
    Views:
    269
    Gunnar Hjalmarsson
    Apr 28, 2005
  4. ShaunJ
    Replies:
    10
    Views:
    193
    Ilya Zakharevich
    Mar 16, 2008
  5. pinkisntwell
    Replies:
    4
    Views:
    107
    Jürgen Exner
    Nov 9, 2009
Loading...

Share This Page