Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
962 views
in Technique[技术] by (71.8m points)

regex - Dollar sign in regular expression and new line character

I know that the dollar sign is used to match the character at the end of the string, to make sure that search does not stop in the middle of the string but instead goes on till the end of the string.

But how does it deal with the newline character, does it match just before the new line character or does it take that into account.

I checked it in eclipse regex, for a regex matching array of strings ([A-Za-z ]+)$ worked, not the other way around ([A-Za-z ]+ )$

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Note that ^ and $ are zero-width tokens. So, they don't match any character, but rather matches a position.

  • ^ matches the position before the first character in a string.
  • $ matches the position before the first newline in the string.

So, the String before the $ would of course not include the newline, and that is why ([A-Za-z ]+ )$ regex of yours failed, and ([A-Za-z ]+)$ succeeded.

In simple words, your $ should be followed by a newline, and no other character.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...