锚(anchor)。锚不与字符串开头与末尾匹配,而与行首和行尾匹配。
string = "abc\ndef\nghi" /^def/ =~ string # 4 /def$/ =~ string # 4
而\A 和\Z 匹配字符窜本身的开头和末尾。\Z 可以匹配最后一个换行符,而\z 不可以。
string = "abc\ndef\nghi" /\Adef/ =~ string # nil /def\Z/ =~ string # nil /\Aabc/ =~ string # 0 /ghi\Z/ =~ string # 8 str2 << "\n" /ghi\Z/ =~ string # 8 /ghi\z/ =~ string # nil
\b 匹配单词边界,\B 匹配非单词边界的位置。
str = "this is a test" str.gsub(/\b/, "|") # "|this| |is| |a| |test|" str.gsub(/\B/, "-") # "t-h-i-s i-s a t-e-s-t"
量词符
/x{5}/ # 5个x /x{5,7}/ # 5到7个x /x{,8}/ # 最多8个x,会有警告 /x{3,}/ # 最少3个x
贪婪
str = "Where the sea meets the moon-blanch'd land," match = /.*?the/.match(str) p match[0] # 结果是Where the sea meets the,而不是Where the。
正负预查
s1 = "New World Dictionary" s2 = "New World Symphony" s3 = "New World Order" reg = /New World(?= Dictionary| Symphony)/ # 正预查 m1 = reg.match(s1) m1.to_a[0] # "New World" m2 = reg.match(s2) m2.to_a[0] # "New World" m3 = reg.match(s3) # nil reg2 = /New World(?! Symphony)/ # 负预查 m1 = reg2.match(s1) m1.to_a[0] # "New World" m2 = reg2.match(s2) # nil m3 = reg2.match(s3) m3.to_a[0] # "New World"
非捕获组
str = "a123b45c678" str.sub(/(a\d+)(?:b\d+)(c\d+)/, "1st=\\1, 2nd=\\2, 3rd=\\3") # "1st=a123, 2nd=c678, 3rd="
内嵌子表达式。可以用来防止贪婪。
str = "abccccdef" re1 = /(abc*)cdef/ re2 = /(?>abc*)cdef/ re1 =~ str # 0 re2 =~ str # nil,子表达式使用了全部的c