[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. Advertisements

  2. SL

    Steven Kuo Guest

    (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.
     
    Steven Kuo, Feb 17, 2005
    #2
    1. Advertisements

  3. SL

    SL Guest

    @@ I want this substrings :
    Yes, sorry.
    Yes, it's perfect. Thank you very much.

    Regards,
    SL
     
    SL, Feb 17, 2005
    #3
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.