Regex 价值<;手柄>;构造可以是;“0”;;在perltestscript.pl第16行使用defined()进行测试
我的代码看起来像这样。有人知道我如何测试这个错误,或者为什么我会得到这个错误吗?问题出现在第16行,这是上面的括号Regex 价值<;手柄>;构造可以是;“0”;;在perltestscript.pl第16行使用defined()进行测试,regex,perl,Regex,Perl,我的代码看起来像这样。有人知道我如何测试这个错误,或者为什么我会得到这个错误吗?问题出现在第16行,这是上面的括号chomp$stringtoFind:在我的代码中: #!usr/bin/perl use strict; use warnings; my $syslogFile = 'syslog'; open (my $syslogInfo, '<', $syslogFile) or die "Could not open $syslogFile"; my $keywordFile
chomp$stringtoFind
:在我的代码中:
#!usr/bin/perl
use strict;
use warnings;
my $syslogFile = 'syslog';
open (my $syslogInfo, '<', $syslogFile) or die "Could not open $syslogFile";
my $keywordFile = 'keyword';
open (my $keywordInfo, '<', $keywordFile) or die "Could not open $keywordFile";
while (my $line = <$syslogInfo>)
{
if(my $stringtoFind =~ <$keywordInfo>)
{
chomp $stringtoFind;
#print "$line\n";
$stringtoFind =~ ( ^/[a-zA-Z][a-zA-Z][a-zA-Z]\s(\d\d)\s(\d\d):(\d\d):(\d\d)\s[a-zA-Z]*\s($stringtoFind).*/s);
open(outputFile, ">>output");
flock(outputFile, 2);
print outputFile "$line\n";
}
}
#!usr/bin/perl
严格使用;
使用警告;
my$syslogFile='syslog';
open(my$syslogInfo),将正则表达式锚定到行首的正确方法是将^
作为表达式的第一个字符(即在第一个/
之后)
$stringtoFind=~(/^[a-zA-Z][a-zA-Z][a-zA-Z]\s(\d\d)\s(\d\d):(\d\d)\s[a-zA-Z]*\s($stringtoFind)。*/s;
如果(my$stringtoFind=~)
=~
是模式匹配运算符,则这也是一个错误。因此,此行没有任何意义,因为它与新创建的变量内的字符串相匹配,
正在从文件句柄读取行,不应这样使用
if(已定义(my$stringtoFind=)
脚本为我编译
您得到的错误是来自
的测试。文件中的一行可以是0
,在perl中计算为false
。因此,在检查eof
时,您应该使用定义的进行测试
您的脚本将使syslog文件的每一行与关键字文件中的匹配行匹配,因为您从关键字文件中为syslog文件的每一行读取一行。因此,syslog的第3行将与关键字的第3行匹配。如果关键字文件的行数较少,则该行将完全不匹配。这可能不是您想要的。您可以打赌在开始循环之前,请停止将关键字文件中的所有行读取到数组中,然后对syslog文件中的每一行进行迭代。如果使用类似的方法,甚至可以创建一个正则表达式来匹配这些行,因此无需迭代
我试图在perl中找到与此python代码等效的代码:
re.search(r'\:\s[^^]+\S', string, re.I|re.M)
那么,了解python代码的作用可能会有所启发:
import re
test_strs = [
": abc^ X",
"\: abc^ X",
]
for test_str in test_strs:
if re.match(r'\:\s[^^]+\S', test_str):
print(test_str)
--output:--
: abc^ X
注意事项:
在正则表达式中,:
不是特殊字符,因此不需要转义,因此正则表达式中的\:
仅相当于:
不区分大小写的搜索,re.I
,没有效果,因为正则表达式没有指定任何大写或小写字母
多行标志re.M
无效,因为正则表达式不尝试使用^
或$
来匹配字符串的开头或结尾
因此,正则表达式可以简化为:
re.search(r':\s[^^]+\S', string)
在perl中,正则表达式是相同的:
string =~ /:\s[^^]+\S/
如果要添加无效的标志,可以编写:
string =~ /:\s[^^]+\S/im
所以问题是,在perl中是否有我看不到的^^^等价物
在书中找到关于它的任何东西
正则表达式的以下部分:
[^^]+
内容如下:
[ ] => This is known as a 'character class', and it means: match
a single character if it is one of the characters listed
inside the brackets
[^ ] => A 'negated character class'. Match a single character if it
is not one of the characters listed inside the brackets
[^^] => What are the characters inside the 'negated character
class' [^ ]? That's simply (the second) ^, so the
whole thing means: match any single character that
is not a ^.
[^^]+ => Match a single character that is not a ^, one or more times
因为^
字符没有出现在日志文件中,所以regex的这一部分实际上意味着:匹配任何字符,一次或多次。因此,我必须执行@line和@stringtoFind,而不是执行我的$line和$stringtoFind,我必须执行@line和@stringtoFind?还有一个问题是,结尾的。*/s确实匹配了我不想要的每一行,并且我试图找到与此python代码等效的代码“re.search(r'\:\s[^^^]+\s',string,re.I|re.m)”在perl中。所以问题是,在perl中是否有^^^等价物,因为我在书中似乎找不到任何关于它的内容?没有。这不会解决问题。您需要单独读取文件,而不是读取syslog文件行中的关键字file。关于正则表达式的问题应该在单独的问题中提出。您不能读取空的文件中的字符串。如果没有剩余字符,则
返回'undeforry,这是stackoverflow的新功能,刚刚接受了答案,再次感谢
[ ] => This is known as a 'character class', and it means: match
a single character if it is one of the characters listed
inside the brackets
[^ ] => A 'negated character class'. Match a single character if it
is not one of the characters listed inside the brackets
[^^] => What are the characters inside the 'negated character
class' [^ ]? That's simply (the second) ^, so the
whole thing means: match any single character that
is not a ^.
[^^]+ => Match a single character that is not a ^, one or more times