Perl中\z和\Z以及\a和\A的区别 [英] Difference between \z and \Z and \a and \A in Perl

查看:79
本文介绍了Perl中\z和\Z以及\a和\A的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能告诉我 \z\Z 以及 \a\A 在 Perl 中用一个简单的例子?

Can you please tell me the difference between \z and \Z as well as \a and \A in Perl with a simple example ?

推荐答案

\z 只匹配字符串的最末尾.

\z only matches the very end of the string.

\Z 也匹配字符串的最末尾,但如果字符串以换行符结尾,则 \Z 也匹配紧接在换行符之前的位置.

\Z also matches the very end of the string, but if the string ends with a newline, then \Z also matches immediately before the newline.

例如,这五个是正确的:

So, for example, these five are true:

'foo' =~ m/foo\z/
'foo' =~ m/foo\Z/
"foo\n" =~ m/foo\Z/
"foo\n" =~ m/foo\n\z/
"foo\n" =~ m/foo\n\Z/

而这个是错误的:

"foo\n" =~ m/foo\z/

它们都与 $ 的不同之处在于它们不受 /m 多行"标志的影响,它允许 $ 在任何一行的结尾.

They both differ from $ in that they are not affected by the /m "multiline" flag, which allows $ to match at the end of any line.

\a 表示警报(铃)字符;它在正则表达式中没有任何额外的特殊含义.

\a denotes the alert (bell) character; it doesn't have any additional special meaning in a regex.

\A 仅匹配字符串的开头.与 \z\Z 一样,与 ^ 不同的是,它不受 /m 多行"标志的影响.

\A matches only at the start of a string. Like \z and \Z, and unlike ^, it's not affected by the /m "multiline" flag.

所有这些都记录在 perlre,Perl 正则表达式手册页:http://perldoc.perl.org/perlre.html.

All of this is documented in perlre, the Perl regular expressions manual page: http://perldoc.perl.org/perlre.html.

这篇关于Perl中\z和\Z以及\a和\A的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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