Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...