[regexp] capturing many substrings

Discussion in 'Perl Misc' started by SL, Feb 16, 2005.

  1. SL

    SL Guest

    Hi,

    I want to capture several substrings of a string: I want each occurrence of
    "/" (a syllabus boundary), with the character until the next "V" and the
    previous "V" included.

    For instance, for this string :
    $string="OF/LOV/FOGVOTO/LFO";

    I want this substrings :
    #OF/LOV (from beginnig to first "V" after the first "/")
    #V/FOGV (from the first "V" before to the first "V" after)
    #V/OTO/LFO (from the first "V" before the end)

    I try this :
    @syl_boundaries = ($string =~ m|(V?[^V]*/[^V]*V?)|g);

    But it returns :
    OF/LOV
    /FOGV
    OTO/LFO
    instead, as if the same character may not be captured twice. Any advice
    would be very helpful!

    Many thanks,
    SL
     
    SL, Feb 16, 2005
    #1
    1. Advertising

  2. SL

    Steven Kuo Guest

    On Thu, 17 Feb 2005, SL wrote:

    > Hi,
    >
    > I want to capture several substrings of a string: I want each occurrence of
    > "/" (a syllabus boundary), with the character until the next "V" and the
    > previous "V" included.
    >
    > For instance, for this string :
    > $string="OF/LOV/FOGVOTO/LFO";
    >
    > I want this substrings :
    > #OF/LOV (from beginnig to first "V" after the first "/")
    > #V/FOGV (from the first "V" before to the first "V" after)
    > #V/OTO/LFO (from the first "V" before the end)
    >
    > I try this :
    > @syl_boundaries = ($string =~ m|(V?[^V]*/[^V]*V?)|g);


    (snipped)



    Perhaps something like:

    my $string = "OF/LOV/FOGVOTO/LFO";

    while ($string =~ m#\G(.*?/.*?)(?=(V|$))#g ) {
    print "$1$2\n";
    }

    See 'perldoc perlre' regarding the \G and look-ahead assertions.

    --
    Hope this helps,
    Steven
     
    Steven Kuo, Feb 17, 2005
    #2
    1. Advertising

  3. SL

    SL Guest

    > @@ I want this substrings :
    > @@ #OF/LOV (from beginnig to first "V" after the first "/")
    > @@ #V/FOGV (from the first "V" before to the first "V" after)
    > @@ #V/OTO/LFO (from the first "V" before the end)
    >
    > I guess you want "VOTO/LFO", as "V/OTO/LFO" isn't a substring.


    Yes, sorry.

    > You can't match the same character twice using m//g, unless you use
    > a trick:
    >
    > m!(?=((?:^|V)[^V]*/[^V]*(?:V|$)))!g
    >
    > seems to do what you want:


    Yes, it's perfect. Thank you very much.

    Regards,
    SL
     
    SL, Feb 17, 2005
    #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. Marco Ippolito
    Replies:
    0
    Views:
    2,619
    Marco Ippolito
    Oct 11, 2004
  2. Joao Silva
    Replies:
    16
    Views:
    403
    7stud --
    Aug 21, 2009
  3. Stuart Moore

    RegExp poser: matching two substrings

    Stuart Moore, Apr 26, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    124
    Anno Siegel
    Apr 27, 2004
  4. Jason S
    Replies:
    1
    Views:
    104
  5. Bart Lateur
    Replies:
    2
    Views:
    89
    Bart Lateur
    Sep 3, 2007
Loading...

Share This Page