ruby中的z与区别

 1 s = "this is
the name
"
 2 puts "--------------"
 3 puts s.match(/name/)
 4 puts s.match(/namez/)
 5 puts "--------------"
 6 
 7 s = "this is
the name"
 8 puts "--------------"
 9 puts s.match(/name/)
10 puts s.match(/namez/)
11 puts "--------------"
1 --------------
2 name
3 
4 --------------
5 --------------
6 name
7 name
8 --------------

 官方文档是

  •  - Matches end of string. If string ends with a newline, it matches just before newline

  • z - Matches end of string

我的理解是和z都是匹配字符串的结尾,但是如果字符串结尾多了一个 ,那么匹配时忽略这个 ,匹配 前面的内容是否与给定的正则匹配,而z不能忽略,所以z会失败,如上面例子

原文地址:https://www.cnblogs.com/or2-/p/6538322.html