Ambiguity about what $ means?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Ambiguity about what $ means?

Pablo Contreras-2
Hi,

I am trying to match midway through a line and then continue to the rest of the file but it seems I am not allowed.

so say I have

=====
1.
2. mary
3.
4. had a little lamb
=====

I am on line 2, right at the beginning

Let's say I do:

s/$/XXX/

I get

=====
1.
2. maryXXX
3.
4. had a little lamb
=====

which you would expect.  'match end-of-line and starting there substitute with XXX'

now let's start from the beginning:

=====
1.
2. mary
3.
4.had a little lamb
=====

I want to do a similar thing but match now just the end-of-line itself but also everything that comes after:

s/$\_.*/XXX/

apparently should match 'end-of-line, then using \_. any char including enf-of-line as many times as necessary to the end of the file.  Then replace with XXX

instead this happens:
'E486: Pattern $\_.* not found'    ?!?

does $ not mean what I think it means?
because oddly when I wrote:

s/\n\_.*/XXX/

it exactly gives:

=====
1.
2. maryXXX
=====

which you would expect.

so seems like $ doesn't really mean end-of-line in certain cases.  I am a bit unclear about this.  Can anybody explain strange behaviour?

Thanks in advance!!
Pablo.

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Ambiguity about what $ means?

Tim Chase
On 2017-03-05 09:20, Pablo Contreras wrote:
> s/$/XXX/
[snip]

> s/$\_.*/XXX/
>
> apparently should match 'end-of-line, then using \_. any char
> including enf-of-line as many times as necessary to the end of the
> file.  Then replace with XXX
>
> instead this happens:
> 'E486: Pattern $\_.* not found'    ?!?
>
> does $ not mean what I think it means?

From

  :help /$

"""
At end of pattern or in front of "\|", "\)" or "\n" ('magic' on):
matches end-of-line <EOL>; at other positions, matches literal '$'.
"""

As it's not the last character in your regexp, it's looking for a
literal "$", not an end-of-line.

-tim




--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Ambiguity about what $ means?

Ben Fritz
On Sunday, March 5, 2017 at 1:55:19 PM UTC-6, Tim Chase wrote:

> On 2017-03-05 09:20, Pablo Contreras wrote:
> > s/$/XXX/
> [snip]
> > s/$\_.*/XXX/
> >
> > apparently should match 'end-of-line, then using \_. any char
> > including enf-of-line as many times as necessary to the end of the
> > file.  Then replace with XXX
> >
> > instead this happens:
> > 'E486: Pattern $\_.* not found'    ?!?
> >
> > does $ not mean what I think it means?
>
> From
>
>   :help /$
>
> """
> At end of pattern or in front of "\|", "\)" or "\n" ('magic' on):
> matches end-of-line <EOL>; at other positions, matches literal '$'.
> """
>
> As it's not the last character in your regexp, it's looking for a
> literal "$", not an end-of-line.
>
But, you can still match end-of-line in the middle of a pattern using "\n".

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Ambiguity about what $ means?

Shawn H Corey
On Mon, 6 Mar 2017 08:01:58 -0800 (PST)
Ben Fritz <[hidden email]> wrote:

> But, you can still match end-of-line in the middle of a pattern using
> "\n".

No, that matches a new-line character. The difference is that $ matches
the end of the line, not a character. This is called an anchor.

"Anchors are a different breed. They do not match any character at all.
Instead, they match a position before, after, or between characters."
http://www.regular-expressions.info/anchors.html


--
Don't stop where the ink does.

        Shawn H Corey

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Ambiguity about what $ means?

Tim Chase
In reply to this post by Ben Fritz
On 2017-03-06 08:01, Ben Fritz wrote:

> On Sunday, March 5, 2017 at 1:55:19 PM UTC-6, Tim Chase wrote:
>> On 2017-03-05 09:20, Pablo Contreras wrote:
>>> s/$/XXX/
>> [snip]
>>> s/$\_.*/XXX/
>> From
>>
>>   :help /$
>>
>> """
>> At end of pattern or in front of "\|", "\)" or "\n" ('magic' on):
>> matches end-of-line <EOL>; at other positions, matches literal
>> '$'. """
>>
>> As it's not the last character in your regexp, it's looking for a
>> literal "$", not an end-of-line.
>
> But, you can still match end-of-line in the middle of a pattern
> using "\n".

Which the OP mentions trying/using:

> On 2017-03-05 09:20, Pablo Contreras wrote:
> because oddly when I wrote:
>
> s/\n\_.*/XXX/
>
> it exactly gives:
>
> =====
> 1.
> 2. maryXXX
> =====
>
> which you would expect.


:-)

-tim



--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Ambiguity about what $ means?

Bram Moolenaar
In reply to this post by Shawn H Corey

Shawn Corey wrote:

> On Mon, 6 Mar 2017 08:01:58 -0800 (PST)
> Ben Fritz <[hidden email]> wrote:
>
> > But, you can still match end-of-line in the middle of a pattern using
> > "\n".
>
> No, that matches a new-line character. The difference is that $ matches
> the end of the line, not a character. This is called an anchor.
>
> "Anchors are a different breed. They do not match any character at all.
> Instead, they match a position before, after, or between characters."
> http://www.regular-expressions.info/anchors.html

Using '\n' does not match a character.

Don't try to apply terminology from some other regexp engine to Vim, it
will just confuse you.  In Vim terminology there is no anchor.

--
hundred-and-one symptoms of being an internet addict:
72. Somebody at IRC just mentioned a way to obtain full motion video without
    a PC using a wireless protocol called NTSC, you wonder how you never
    heard about it

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Ambiguity about what $ means?

Nikolay Aleksandrovich Pavlov
2017-03-06 23:09 GMT+03:00 Bram Moolenaar <[hidden email]>:

>
> Shawn Corey wrote:
>
>> On Mon, 6 Mar 2017 08:01:58 -0800 (PST)
>> Ben Fritz <[hidden email]> wrote:
>>
>> > But, you can still match end-of-line in the middle of a pattern using
>> > "\n".
>>
>> No, that matches a new-line character. The difference is that $ matches
>> the end of the line, not a character. This is called an anchor.
>>
>> "Anchors are a different breed. They do not match any character at all.
>> Instead, they match a position before, after, or between characters."
>> http://www.regular-expressions.info/anchors.html
>
> Using '\n' does not match a character.
>
> Don't try to apply terminology from some other regexp engine to Vim, it
> will just confuse you.  In Vim terminology there is no anchor.

For reference: Vim documentation names such things “zero-width match”
(`:h /zero-width`). `\n` may match a character if you use it in
functions like `substitute()` (where pattern applies to one of the
string arguments), but when searching a buffer this is some kind of
pseudocharacter:

1. not zero-width because you can join lines using `s/\n//`,
2. also matches at the very end of the buffer even if `&endofline` is
set to zero (designating that last line in the buffer should not end
with a LF/CRLF/CR)
3. matches line end always, regardless of &fileformat setting (meaning
that “physically” file may contain \n, \r\n or \r when being written)
4. internally there are no characters corresponding to a line end:
each line is a NUL-terminating C string.

>
> --
> hundred-and-one symptoms of being an internet addict:
> 72. Somebody at IRC just mentioned a way to obtain full motion video without
>     a PC using a wireless protocol called NTSC, you wonder how you never
>     heard about it
>
>  /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
> ///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
> \\\  an exciting new programming language -- http://www.Zimbu.org        ///
>  \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///
>
> --
> --
> 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 the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> For more options, visit https://groups.google.com/d/optout.

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Ambiguity about what $ means?

Tony Mechelynck
On Mon, Mar 6, 2017 at 9:20 PM, Nikolay Aleksandrovich Pavlov
<[hidden email]> wrote:
[...]

> For reference: Vim documentation names such things “zero-width match”
> (`:h /zero-width`). `\n` may match a character if you use it in
> functions like `substitute()` (where pattern applies to one of the
> string arguments), but when searching a buffer this is some kind of
> pseudocharacter:
>
> 1. not zero-width because you can join lines using `s/\n//`,
> 2. also matches at the very end of the buffer even if `&endofline` is
> set to zero (designating that last line in the buffer should not end
> with a LF/CRLF/CR)
> 3. matches line end always, regardless of &fileformat setting (meaning
> that “physically” file may contain \n, \r\n or \r when being written)
> 4. internally there are no characters corresponding to a line end:
> each line is a NUL-terminating C string.

Well said.
In short:
• $ (a zero-width pattern atom) matches the end of a line at the end
of a pattern (or before \| etc.)
• \n matches an end-of-line anywhere in a pattern, regardless of how
that end-of-line is represented. (Internally all lines have an
end-of-line, which IIUC is the null at the end of a C string; 'noeol'
is only relevant when writing a 'binary' or 'nofixeol' file.)
• But to _insert_ a line break by means of a :substitute command, we
must use \r not \n so the following is one of several possible ways to
count lines (between two marks, in this example) without changing
them:

     :'a,'b-1s/\n/\r/

Best regards,
Tony.

--
--
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 the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.