matchstr() ^ and $ wrong for multi-line text?

已查看 64 次
跳至第一个未读帖子

David Barnett

未读,
2014年2月9日 01:15:532014/2/9
收件人 vim...@googlegroups.com
I noticed that matchstr() seems to always interpret ^ as start-of-string and $ as end-of-string, even when the string is multiple lines. ":help pattern" says they should be start-of-line and end-of-line, respectively.

For example
:echo matchstr("foo\nbar", '^foo$')
matches nothing.

I don't see anything in the matchstr() help indicating that it treats start-of-line and end-of-line any differently. I tried \_^ and \_$ as well. Is this a bug?

David

John Beckett

未读,
2014年2月9日 05:15:442014/2/9
收件人 vim...@googlegroups.com
David Barnett wrote:
> I noticed that matchstr() seems to always interpret ^ as
> start-of-string and $ as end-of-string, even when the string
> is multiple lines. ":help pattern" says they should be
> start-of-line and end-of-line, respectively.
>
> For example
> :echo matchstr("foo\nbar", '^foo$')
> matches nothing.

:help matchstr() says it's the same as match(), and the help for
that says to see ':help string-match', and that says that "\n"
can be matched with "\n", but (by implication) not by $.

John


David Barnett

未读,
2014年2月9日 11:21:582014/2/9
收件人 vim...@googlegroups.com
Oh yeah, huh:
  Don't forget that "^" will only match at the first character of the String and 
  "$" at the last character of the string.  They don't match after or before a   
  "\n".

Guess I just lost the scavenger hunt…

David




--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to a topic in the Google Groups "vim_use" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/eX0WwxcMj8o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vim_use+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

ZyX

未读,
2014年2月9日 13:08:162014/2/9
收件人 vim...@googlegroups.com

You do not have multiline string here. See :h NL-used-for-Nul: regex engine always assumes that newline character is a mere replacement for zero byte and newline is always represented by a string end. Which is true for buffers. Not for buffers there is only `readfile()`/`getline(start, end)` format (which is a list of lines where newlines are used for nuls) which match* functions, substitute(), =~# and so on do not understand.

>
> David

回复全部
回复作者
转发
0 个新帖子