Regex 匹配除包含'_智商';
编辑:对不起!我以前应该提到这一点。在您的回答中,请不要假设下面列出的项目是我正在搜索的目录中唯一的内容。这些是我想要匹配的东西,除了指定的两个。谢谢 这应该很容易,但我尝试的一切都没有达到目的。我有以下需要匹配的项目:Regex 匹配除包含'_智商';,regex,string,perl,Regex,String,Perl,编辑:对不起!我以前应该提到这一点。在您的回答中,请不要假设下面列出的项目是我正在搜索的目录中唯一的内容。这些是我想要匹配的东西,除了指定的两个。谢谢 这应该很容易,但我尝试的一切都没有达到目的。我有以下需要匹配的项目: bodipr2__ds_di_uat bodipr2__ds_dw_uat bodipr2__ds_iq_uat bodipr2__ds_iq_uat_back bodipr2__ds_itsys_uat bodipr2__ds_ppp_uat bodipr2__ds_psd_
bodipr2__ds_di_uat
bodipr2__ds_dw_uat
bodipr2__ds_iq_uat
bodipr2__ds_iq_uat_back
bodipr2__ds_itsys_uat
bodipr2__ds_ppp_uat
bodipr2__ds_psd_uat
bodipr2__ds_usage_uat
bodits2__ds_pef_tst
bodits2__ds_ppp_tst
bodits2__ds_pri_tst
除了我想省略包含\u iq\u
的两个。所以我写了一个这样的表达:
bodi.*?__ds[^_iq_]
但这忽略了所有这些。如果我尝试这样做:
bodi.*?__ds_[^iq]
这省略了\u itsys
一个。我猜它匹配的是不包含“I”或“q”的所有内容。我希望它省略在该位置不包含“iq”
的内容
我很尴尬,我甚至不得不问这个问题,但如果有人能给我指出正确的方向,我将非常感激。您可以使用
而不是来反转:
if (not /^bodi.*_iq_.*/) {
print;
}
或:
如果存在匹配项,您可以跳过,然后处理其余项:
#!/usr/bin/env perl
use strict;
use warnings;
while (<DATA>) {
next if /^bodipr2__ds_iq_/; # Skip if bodipr2__ds_iq_ is matched
# Process data
print;
}
__DATA__
bodipr2__ds_di_uat
bodipr2__ds_dw_uat
bodipr2__ds_iq_uat
bodipr2__ds_iq_uat_back
bodipr2__ds_itsys_uat
bodipr2__ds_ppp_uat
bodipr2__ds_psd_uat
bodipr2__ds_usage_uat
bodits2__ds_pef_tst
bodits2__ds_ppp_tst
bodits2__ds_pri_tst
试试这个:bod[a-z\d]+\uuds\u(?!iq\uw++
我已经从测试过了,这个怎么样:
use strict;
use warnings;
for (<DATA>)
{
chomp;
if (! m/bodi.*_iq_/)
{
print $_ . "\n";
}
}
__DATA__
bodipr2__ds_di_uat
bodipr2__ds_dw_uat
bodipr2__ds_iq_uat
bodipr2__ds_iq_uat_back
bodipr2__ds_itsys_uat
bodipr2__ds_ppp_uat
bodipr2__ds_psd_uat
bodipr2__ds_usage_uat
bodits2__ds_pef_tst
bodits2__ds_ppp_tst
bodits2__ds_pri_tst
使用严格;
使用警告;
对于()
{
咀嚼;
如果(!m/bodi..*iq./)
{
打印$。“\n”;
}
}
__资料__
bodipr2_uuds_udi_uuat
bodipr2_uuds_udw_uuat
bodipr2_uuds_uiq_uuat
bodipr2_uuds_uiq_uuat_u后
bodipr2_uuds_uitsys_uuat
bodipr2_uuds_uPPP_uUAT
bodipr2_uuds_uPSD_uUAT
bodipr2的使用
紧身胸衣2
紧身胸衣2
紧身胸衣2
这段代码:
while ($subject =~ m/^(?:(?!_iq_).)*$/g) {
print $&, "\n";
}
将打印除这两个字符串以外的所有内容。您可以通过在(?!\u iq)
之后添加更多负面外观来扩展此功能
示例测试:
我看到了您的编辑,不确定这是否会排除您希望包含的内容,但我尝试了这个,它对我有效
#!/usr/bin/perl
use strict;
use warnings;
my @words = qw(bodipr2__ds_di_uat
bodipr2__ds_dw_uat
bodipr2__ds_iq_uat
bodipr2__ds_iq_uat_back
bodipr2__ds_itsys_uat
bodipr2__ds_ppp_uat
bodipr2__ds_psd_uat
bodipr2__ds_usage_uat
bodits2__ds_pef_tst
bodits2__ds_ppp_tst
bodits2__ds_pri_tst);
print "Original data\n\n";
foreach my $print (@words)
{
print "$print\n";
}
print "\nNew data\n\n";
foreach my $word (@words)
{
next if $word =~ /bodi[a-z]+\d__[a-z]+_iq_[a-z]+/;
print "$word\n";
}
为了解释为什么你尝试的([^iq]
)不起作用,这是因为[^iq]
的意思是“匹配任何一个字符,除非该字符是“i”或“q”。而[^iq]
的意思是相同的,除非它也匹配“\uq”
所以,在你的情况下
bodi.*.\uU ds[^\u iq.]
会100%匹配,因为每个sring在“ds”之后都包含“\uu”,这将匹配[^\u iq.]
bodi.*.\uu ds.[^iq]
将只匹配在“ds”之后包含字母“i”或“q”的行,正如您现在所猜测的那样,这两行都是\uiq
和\uitsys
行
匹配“不包含iq”的正确方法是消极前瞻(?!iq)
显示在Godspeed或FailedDev的答案中。对不起,我会把我的问题修正得更具体一些,但是我匹配的目录中还有其他一些东西根本不匹配,这会导致这些东西被过滤掉……我正在使用的grep
函数没有办法过滤掉吗?太棒了,确实如此匹配。让我快速测试它,当我确认它在我的脚本中有效时,我将接受:)
while ($subject =~ m/^(?:(?!_iq_).)*$/g) {
print $&, "\n";
}
#!/usr/bin/perl
use strict;
use warnings;
my @words = qw(bodipr2__ds_di_uat
bodipr2__ds_dw_uat
bodipr2__ds_iq_uat
bodipr2__ds_iq_uat_back
bodipr2__ds_itsys_uat
bodipr2__ds_ppp_uat
bodipr2__ds_psd_uat
bodipr2__ds_usage_uat
bodits2__ds_pef_tst
bodits2__ds_ppp_tst
bodits2__ds_pri_tst);
print "Original data\n\n";
foreach my $print (@words)
{
print "$print\n";
}
print "\nNew data\n\n";
foreach my $word (@words)
{
next if $word =~ /bodi[a-z]+\d__[a-z]+_iq_[a-z]+/;
print "$word\n";
}