Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
865 views
in Technique[技术] by (71.8m points)

regex - Read file content and use Regular Expression to locate a pattern in each File in Perl

I have about 200 files located in the same directory, all of which contain a specific piece of content that I need to match using RegExp and either save all of the matched contents into a single array or store them in a new file.

When working with notepad++ regexp engine I do the following to locate the pattern:

<div class="opacity description">(.*)</div>

so that is the pattern I am looking for.

And this is how i Open and List all the files in the directory.

my $d = shift;

opendir(D, "details/") || die "Can't opedir $d: $!
";
my @list = readdir(D);
closedir(D);

foreach my $f (@list) {
  print "$f = $f
";
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
use strict;
use warnings;

use HTML::TreeBuilder::XPath;

my ($dir) = @ARGV;

my @files = glob "$dir/*";

for my $file (@files) {
  my $tree = HTML::TreeBuilder::XPath->new_from_file($file);
  my @opacity = $tree->findnodes_as_strings('//div[@class="opacity description"]');
  print "
$file
";
  print "  $_
" for @opacity;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...