"\n" matches both '[\n]' and '[^\n]'

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

"\n" matches both '[\n]' and '[^\n]'

Nikolay Aleksandrovich Pavlov
While trying to purge comment lines inside a variable, I encountered the
following problem:

    :echo substitute("a\nb", '[\n]', '<NL>', 'g')
    a<NL>b
    :echo substitute("a\nb", '[^\n]', '<!NL>', 'g')
    <!NL><!NL><!NL>

app-editors/vim-7.2.303 (Gentoo amd64),
not fixed in
app-editors/vim-7.2.442 (Gentoo ~amd64)

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: "\n" matches both '[\n]' and '[^\n]'

John Little-4
On Jun 21, 7:49 am, ZyX <[hidden email]> wrote:
> While trying to purge comment lines inside a variable, I
> encountered the following problem:
>
>     :echo substitute("a\nb", '[\n]', '<NL>', 'g')
>     a<NL>b
>     :echo substitute("a\nb", '[^\n]', '<!NL>', 'g')
>     <!NL><!NL><!NL>

:help /collection says

     The end-of-line is also matched when the collection starts with
     "^"!  Thus "\_[^ab]" matches the end-of-line and any character
     but "a" and "b".  This makes it Vi compatible: Without the "\_"
     or "\n" the collection does not match an end-of-line.

I suspect the behaviour you've noticed is a consequence of this
reasoning.  Whether it should apply to newlines within the text is
beyond me presently, I'd have to consider lots of uses, consume
much coffee, and scratch my head till it bleeds.

HTH, John

--
You received this message from the "vim_dev" 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
Reply | Threaded
Open this post in threaded view
|

Re: "\n" matches both '[\n]' and '[^\n]'

Tony Mechelynck
In reply to this post by Nikolay Aleksandrovich Pavlov
On 20/06/10 21:49, ZyX wrote:

> While trying to purge comment lines inside a variable, I encountered the
> following problem:
>
>      :echo substitute("a\nb", '[\n]', '<NL>', 'g')
>      a<NL>b
>      :echo substitute("a\nb", '[^\n]', '<!NL>', 'g')
>      <!NL><!NL><!NL>
>
> app-editors/vim-7.2.303 (Gentoo amd64),
> not fixed in
> app-editors/vim-7.2.442 (Gentoo ~amd64)

I also see this (which, from the help paragraph mentioned by John, might
be intentional) in Vim 7.3a. However, after some experimenting, I found
how not to match a linefeed:

        :echo substitute("a\nb", '[^\d0]', '<!NL>', 'g')
        <!NL>
        <!NL>

see :help NL-used-for-Nul


Best regards,
Tony.
--
Then here's to the City of Boston,
The town of the cries and the groans.
Where the Cabots can't see the Kabotschniks,
And the Lowells won't speak to the Cohns.
                -- Franklin Pierce Adams

--
You received this message from the "vim_dev" 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
Reply | Threaded
Open this post in threaded view
|

Re: "\n" matches both '[\n]' and '[^\n]'

Nikolay Aleksandrovich Pavlov
Ответ на сообщение «Re: "\n" matches both '[\n]' and '[^\n]'»,
присланное в 03:12:36 21 июня 2010, Понедельник,
отправитель Tony Mechelynck:

I found a way to match <!NL> with [^\x0a], but this makes pattern less readable,
your suggestion is even worse.

Текст сообщения:

> On 20/06/10 21:49, ZyX wrote:
> > While trying to purge comment lines inside a variable, I encountered the
> >
> > following problem:
> >      :echo substitute("a\nb", '[\n]', '<NL>', 'g')
> >
> >      a<NL>b
> >
> >      :echo substitute("a\nb", '[^\n]', '<!NL>', 'g')
> >
> >      <!NL><!NL><!NL>
> >
> > app-editors/vim-7.2.303 (Gentoo amd64),
> > not fixed in
> > app-editors/vim-7.2.442 (Gentoo ~amd64)
>
> I also see this (which, from the help paragraph mentioned by John, might
> be intentional) in Vim 7.3a. However, after some experimenting, I found
>
> how not to match a linefeed:
> :echo substitute("a\nb", '[^\d0]', '<!NL>', 'g')
>
> <!NL>
> <!NL>
>
> see :help NL-used-for-Nul
>
>
> Best regards,
> Tony.
>

signature.asc (205 bytes) Download Attachment