在perl中正则表达式中的OR条件 [英] OR condition in regular expression in perl

查看:230
本文介绍了在perl中正则表达式中的OR条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本,它通过Perl中的正则表达式执行OR函数。我写了一个代码,其中如果字符串包含'D'或'E'后跟'P',它应该打印D或E后跟P,否则D或E不跟P。假设我给$ s ='ABCDEABCDEPABCDEAB'它应该打印else条件,但我认为我的if语句不能正常工作。请帮忙。

I am trying to write a script which performs an OR function through regular expressions in Perl. I have written a code in which if string contains 'D' or 'E' followed by 'P' it should print "D or E followed by P" otherwise "D or E not followed by P". Suppose if I give $s = 'ABCDEABCDEPABCDEAB' it should print the else condition but I think my if statement is not working properly. Please help.

my $s = 'ABCDEABCDEPABCDEAB';
if ($s =~ /D|E(?=P)/) {
    print "D or E is followed by P";
} 
else {
    print "D or E is not followed by P";
}


推荐答案

if ($s =~ /[DE]P/) {
    print "D or E is followed by P";
}
else {
    print "neither D nor E is followed by P";
}

这篇关于在perl中正则表达式中的OR条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆