Will Ryan
Will Ryan

Reputation: 681

Perl - finding multiple substrings in a variable?

I'm very new to Perl and I've been using this site for a few years looking for answers to questions I've had regarding other projects I've been working on but I can't seem to find the answer to this one..

My script receives a HTTP response with data that I need to strip down to parse the correct information.

So far, I have this for the substring..

my $output = substr ($content, index($content, 'Down'), index($content, '>'));

This seems to be doing what it's supposed to be.. finding the word 'Down' and then substringing it up until it finds a >.

However, the string 'Down' can appear many times within the response and this stops looking after it finds the first example of this.

How would I go about getting the substring to be recursive?

Thanks for your help :)

Upvotes: 2

Views: 324

Answers (2)

user1126070
user1126070

Reputation: 5069

Another solution,without iteration and just storing what is down.

use Data::Dumper;
my $x="aa Down aaa > bb Down bbb > cc Down ccc >";
my @downs = $x =~ m!Down([^>]+)>!gis;
print Dumper(\@downs);

Upvotes: 2

Guru
Guru

Reputation: 16994

One way like this:

my $x="aa Down aaa > bb Down bbb > cc Down ccc >";

while ($x =~/(Down[^>]+>)/g){
        print $1;
}

Upvotes: 5

Related Questions