^((?!i[ \t]+am|player).)+i[ \t]+am((?!player).)+$
如果需要排除的player一定在行尾,一楼的答案也行,要是不限定在尾部,任何位置的都不行(比如甚至在i am之前),就得用这个。
关键解释:(?!xxx)——当前位置不是xxx;((?!xxx).)+——当前位置以后一直没有xxx。
#!perl
#
open FILE, "fortest.txt";
while (
{
$line = $_;
print $line;
if($line =~ /.*a.*/ && $line !~ /.*b.*/)
{
$temp .= $line;
}
}
close FILE;
open FILE, ">new.txt";
print FILE $temp;
close FILE;
已测试,可以实现功能
while(<>){print $_ if (/i am/ && !/player/)};
保存为check.pl
check.pl 1.txt
输出结果:
i am a man
i am a teacher
因为有先后顺序的问题,貌似一句话没有办法满足。
#!/usr/bin/perl
#Define develop information here
$ApplicationVerion="1.0";
#Import additional module here
use Getopt::Std;
#Generate application run time variable here
$ApplicationExecutePathAndName=$0;
#Define global constant here
$DataFile="./DataFile.txt";
#Define global variable here
#Define function here
#Main code start here
open(FILE, $DataFile) || die "can not open the file: $DataFile";
while (defined ($Eachline =
{
chomp $Eachline;
if($Eachline=~/i am/)
{
if($Eachline!~/play/)
{
print $Eachline."\n";
}
}
}
close FILE;
#Main code end here
@result = $subject =~ m/^i\sam.+(?
/i am/ && !/player/