user1783150
user1783150

Reputation: 281

Print Perl Regex Matches

In the program I'm working on, I'm attempting to take in a txt file, then print the bits of txt contained in a pair of quotation marks.

Assuming I've taken in the txt file and put it into an array with each line as an array element this is what I was assuming would work, but alas no luck:

txt file contents:
Lorem ipsum dolor sit amet
consectetur "adipisicing elit"
sed "do" eiusmod tempor incididunt
ut "labore et dolore" magna aliqua

CODE:
foreach(@arr)
{
    print $1 if /("*")/g;
}

Output: 
""

Upvotes: 0

Views: 54

Answers (2)

Filippo Lauria
Filippo Lauria

Reputation: 2064

#!/usr/bin/perl

use strict;
use warnings;

foreach(<DATA>) {
    print $1 if /(".*")/;
}

__DATA__
txt file contents:
Lorem ipsum dolor sit amet
consectetur "adipisicing elit"
sed "do" eiusmod tempor incididunt
ut "labore et dolore" magna aliqua

Upvotes: 0

mpapec
mpapec

Reputation: 50637

 ...
foreach (@arr) {
    print $1 for /(".*?")/g;
}
 ...

Upvotes: 2

Related Questions