[regexp] capturing many substrings

S

SL

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
 
S

Steven Kuo

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.
 
S

SL

@@ 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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top