Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有需要检查最后两个字符的字符串。如果发现最后两个字符符合我的要求,我需要截断字符串,否则保持字符串不变。我知道它可以使用控制条件来完成,但我想知道 perl 中是否有任何单个语句可以实际实现这一点。还建议任何更好的方法来做到这一点。
例子:
$str = "Hello";
所以如果最后两个字符是“tr”,我想截断字符串,但在这种情况下,我们需要将字符串保留为原始字符串,因为最后一个字符不是“tr”
Perl 的替换运算符s/// 在这里是理想的。\z匹配字符串结尾,但不匹配行尾。
s///
\z
$str =~ s/tr\z//;