ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:708回复:0

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

楼主#
更多 发布于:2016-05-30 21:31
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: $!\n";
my @list = readdir(D);
closedir(D);

foreach my $f (@list) {
  print "\$f = $f\n";
}
answer(解决方案):

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 "\n$file\n";
  print "  $_\n" for @opacity;
}

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部