OT: regular expression matching multiple occurrences of one group

Discussion in 'Perl Misc' 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
    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:
    851
    Alan Moore
    Dec 2, 2005
  2. Replies:
    7
    Views:
    315
    Larry Bates
    Feb 24, 2006
  3. pinkisntwell
    Replies:
    5
    Views:
    726
    Gabriel Genellina
    Nov 10, 2009
  4. Replies:
    10
    Views:
    285
    Gunnar Hjalmarsson
    Apr 28, 2005
  5. ShaunJ
    Replies:
    10
    Views:
    205
    Ilya Zakharevich
    Mar 16, 2008
Loading...

Share This Page