空行后跟新行的匹配对于 regex 失败,^$^J
但对于 regex 匹配成功^^J
。以前的正则表达式有什么问题?
$
在正则表达式中通常匹配行尾的空字符串,但实际上,只有当 出现$
在正则表达式中的特定位置时(例如,在正则表达式的末尾,或在子组的末尾,IIRC )。如果$
出现在“中间”,则它仅与该$
字符匹配。同样适用于^
. 例如(string-match "a^b$c" "1a^b$c2")
返回 1。
C-hig (emacs) Regexps
记录此行为:
\xe2\x80\x98^\xe2\x80\x99\n is a special character that matches the empty string, but only at\n the beginning of a line in the text being matched. Otherwise it\n fails to match anything. Thus, \xe2\x80\x98^foo\xe2\x80\x99 matches a \xe2\x80\x98foo\xe2\x80\x99 that occurs\n at the beginning of a line.\n\n For historical compatibility reasons, \xe2\x80\x98^\xe2\x80\x99 can be used with this\n meaning only at the beginning of the regular expression, or after\n \xe2\x80\x98\\(\xe2\x80\x99 or \xe2\x80\x98\\|\xe2\x80\x99.\n\n\xe2\x80\x98$\xe2\x80\x99\n is similar to \xe2\x80\x98^\xe2\x80\x99 but matches only at the end of a line. Thus,\n \xe2\x80\x98x+$\xe2\x80\x99 matches a string of one \xe2\x80\x98x\xe2\x80\x99 or more at the end of a line.\n\n For historical compatibility reasons, \xe2\x80\x98$\xe2\x80\x99 can be used with this\n meaning only at the end of the regular expression, or before \xe2\x80\x98\\)\xe2\x80\x99\n or \xe2\x80\x98\\|\xe2\x80\x99.\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
1663 次 |
最近记录: |