且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

perl 精确字符串匹配

更新时间:2022-05-05 23:45:51

我只想使用字符串相等运算符 eq 而不是正则表达式.

I would just use the string equality operator eq instead of a regex.

if( $file_yes_no eq 'yes' ) ...

如果我想让它不区分大小写,我会先用 lc 转换为小写.

If I wanted it case insensitive I'd first convert to lowercase with lc.

你的正则表达式的问题是它会很高兴地匹配任何包含字母yes的字符串.如果你愿意,你可以像这样匹配字符串的开头和结尾:

The problem with your regex is it will happily match any string containing the letters yes sequentially. If you wish, you can match the start and end of the string like this:

if ($file_yes_no =~ m/^yes$/i ) ...

但我个人更喜欢第一个选项.

But I personally prefer the first option.

哦,我错过了第一部分......嗯.同样的交易,如果您必须使用正则表达式.

Oh, I missed the first part... Hmmmm. Same deal, if you must use regex.

m/^(yes|no)$/i

再一次,我更倾向于避免使用正则表达式

Once again I'd be more inclined to avoid regex