问题
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