Why doesnt [\n$] work while (\n|$) does?

守給你的承諾、 提交于 2019-12-23 20:14:32

问题


We have this string (without new line at the end):

The quick brown fox jumps over the lazy dog

I want to match the entire string until new line \n or end $ occurs.

  • I firstly tried: [\n$] - didnt work.
  • Then i tried (\n|$) - did work

Question: Why doesnt [\n$] match the string while (\n|$) does?


回答1:


Because $ in a character class is seen as a literal




回答2:


Another way to look at it: a character class matches exactly one character. The end-of-line matched by $ is an empty string. That's why and end-of-line cannot be matched by a character class.

(As a consequence, the only possible interpretation for $ is the literal.)



来源:https://stackoverflow.com/questions/16887354/why-doesnt-n-work-while-n-does

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!