troubleshooting Question

perl: Extracting text from string

Avatar of trevor1940
trevor1940 asked on
Perl
4 Comments1 Solution297 ViewsLast Modified:
Hi

I have a string of text that should have 1 or more sub string inside brackets whats the best way to extract each wanted sub string, I then need to do stuff with each of these

The code bellow only grabs the first "Wanted"

print "Hello World!\n";

my $text = "Lorem ipsum dolor sit amet,(Wanted 1) consectetur adipiscing elit. (Wanted 2) (Wanted 3) Nunc bibendum ante diam, a imperdiet purus (Wanted 4) faucibus a. Pellentesque ultricies nibh.";

my @WantedText;
if($text =~ m/\((.*?)\)/ )
  {
     push @WantedText, $1;  
  }
  
  foreach my $want (@WantedText){
      
      print $want . "\n";
  }
ASKER CERTIFIED SOLUTION
NorieSenior Associate
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros