mohit
mohit

Reputation: 134

all possible sub strings matching the regex

I have a string

<start><tag1><tag2><tag3><end>

if I have a regex <.*end> it matches the complete string and stops. Is there some way so that it gives all the matching substrings ike

<end>
<tag3><end>
<tag2><tag3><end>
<tag1><tag2><tag3><end>

Upvotes: 2

Views: 64

Answers (2)

Miller
Miller

Reputation: 35218

Use a lookahead assertion to find the matches.

It's often a good idea to avoid strictly zero width expressions, so add sub expression outside the assertion to gobble a single character.

(?=(<.*<end>)).

Demonstrated below:

my $string = '<start><tag1><tag2><tag3><end>';

while ($string =~ /(?=(<.*end>))./g) {
    print $1, "\n";
}

Outputs:

<start><tag1><tag2><tag3><end>
<tag1><tag2><tag3><end>
<tag2><tag3><end>
<tag3><end>
<end>

Upvotes: 2

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89649

Yes, you must wrap your pattern in a lookahead (this is the only way to obtain overlapped results):

(?=(<.*end>))

The result is in the capture group 1.

Upvotes: 4

Related Questions