选项不要问为什么,但...如何使Perl的正则表达式条件
我有需要,如果在Windows上运行,但要区分大小写的正则表达式的情况下* nix上运行敏感。
下面是我现在正在做的一些事情的片段。
sub relative_path
{
my ($root, $path) = @_;
if ($os eq "windows")
{
# case insensitive with regex option 'i'
if ($path !~ /^\Q$root\E[\\\/](.*)$/i)
{
print "\tFAIL:$root not in $path\n";
}
else
{
return $1;
}
}
else
{
# case sensitive
if ($path !~ /^\Q$root\E[\\\/](.*)$/)
{
print "\tFAIL:$root not in $path\n";
}
else
{
return $1;
}
}
return "";
}
唉!重复伤害我的强迫症,但我的perl-fu很弱。不知何故,我想让正则表达式选项'我'不区分大小写的条件,但我现在不怎么样?
你可以以编程方式确定操作系统是否为windows:'my $ is_windows = $^O =〜/ mswin/i;' – 2011-02-28 19:05:41