2

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 :)

1
  • Use a regex match with the /g modifier? while($content =~ /(Down.*?>)/g) { print $1 } Commented Jul 26, 2013 at 11:26

2 Answers 2

5

One way like this:

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

while ($x =~/(Down[^>]+>)/g){
        print $1;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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);

2 Comments

Nice non-recursive example, but it differs a bit from what the original sample does. Having the 'i' flag makes it case insensitive, which might not be desired, and the positioning of the parentheses causes " aaa " to be captured, rather than "Down aaa >" (which may or may not be desired). I'd propose an edit myself, but apparently edits need to be 6 characters or more (but... it's a regex!).
Yes, removing Down and > string from regexp was intentional. "just storing what is down" ie. Not stroing the string down and the >.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.