Vim RE and lazy operator

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

Vim RE and lazy operator

caruso_g
Hi there,
is there a lazy operator for RE in Vim?

I mean, I have the following code:

<li>Support: <a href="[hidden email]"
title="[hidden email]">[hidden email]</a></li>

and I tried to select just the text inside double quotes for
substitutions using: %s/"\(.\+@.\+\)"/\1sometext/g

Obviously, that select the whole text inside the first " on the row
till the last one and not the second one as I would.
Since I am just learning RE, I was wondering if does exist a lazy
operator for Vim RE.

Thank you all.

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Vim RE and lazy operator

Nikolay Aleksandrovich Pavlov
Ответ на сообщение «Vim RE and lazy operator»,
присланное в 20:00:06 30 августа 2010, Понедельник,
отправитель caruso_g:

You mean, non-greedy? For part after @ you can use non-greedy \{-1,} instead of
\+, but this will not help you with part before @. Here you must just replace
dot with [^"@].

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

> Hi there,
> is there a lazy operator for RE in Vim?
>
> I mean, I have the following code:
>
> <li>Support: <a href="[hidden email]"
> title="[hidden email]">[hidden email]</a></li>
>
> and I tried to select just the text inside double quotes for
> substitutions using: %s/"\(.\+@.\+\)"/\1sometext/g
>
> Obviously, that select the whole text inside the first " on the row
> till the last one and not the second one as I would.
> Since I am just learning RE, I was wondering if does exist a lazy
> operator for Vim RE.
>
> Thank you all.

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

Re: Vim RE and lazy operator

Tom Link-3
> You mean, non-greedy? For part after @ you can use non-greedy \{-1,} instead of
> \+, but this will not help you with part before @. Here you must just replace
> dot with [^"@].

This one should work too:
s/"\zs.\{-}@.\{-}\ze"/.../g

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Vim RE and lazy operator

Nikolay Aleksandrovich Pavlov
Ответ на сообщение «Re: Vim RE and lazy operator»,
присланное в 20:52:18 30 августа 2010, Понедельник,
отправитель Tom Link:

You are sure? Quick test:
    :echo matchstr('"", "abc@def", ""', '"\zs.\{-}@.\{-}\ze"')
should echo ``abc@def'', but echoes ``", "abc@def''.

Текст сообщения:
> > You mean, non-greedy? For part after @ you can use non-greedy \{-1,}
> > instead of \+, but this will not help you with part before @. Here you
> > must just replace dot with [^"@].
>
> This one should work too:
> s/"\zs.\{-}@.\{-}\ze"/.../g

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

Re: Vim RE and lazy operator

Christian Brabandt
Hi ZyX!

On Mo, 30 Aug 2010, ZyX wrote:

> You are sure? Quick test:
>     :echo matchstr('"", "abc@def", ""', '"\zs.\{-}@.\{-}\ze"')
> should echo ``abc@def'', but echoes ``", "abc@def''.

I am not sure, what you tested, but this works as expected:

:echo matchstr('"abc@def"', '"\zs.\{-}@.\{-}\ze"')
abc@def


regards,
Christian

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Vim RE and lazy operator

Nikolay Aleksandrovich Pavlov
Ответ на сообщение «Re: Vim RE and lazy operator»,
присланное в 22:10:16 30 августа 2010, Понедельник,
отправитель Christian Brabandt:

The problem is that you can have more then one quote before @. If there is more
then one quote then you will capture everything starting from the first quote,
test your regex with original string provided by caruso_g, but add the attribute
``class="Test"'' to <li> tag, it will return
    Test">Support: <a href="[hidden email]
This is not the desired behavior.

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

> Hi ZyX!
>
> On Mo, 30 Aug 2010, ZyX wrote:
> > You are sure? Quick test:
> >     :echo matchstr('"", "abc@def", ""', '"\zs.\{-}@.\{-}\ze"')
> >
> > should echo ``abc@def'', but echoes ``", "abc@def''.
>
> I am not sure, what you tested, but this works as expected:
> :echo matchstr('"abc@def"', '"\zs.\{-}@.\{-}\ze"')
>
> abc@def
>
>
> regards,
> Christian

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

Re: Vim RE and lazy operator

caruso_g
> The problem is that you can have more then one quote before @. If there is more
> then one quote then you will capture everything starting from the first quote,

ZyX is right, testing Tom's expression "\zs.\{-}@.\{-}\ze" with the
code <a class="some class" href="mailto:[hidden email]" title="some
title">link</a>
has been selected the string

    some class" href="mailto:[hidden email]

Thanks to you all, though (btw, I am sorry, I always try to look for
help in the manual, but I didn't know the english term to search, in
this case: "h non-greedy")
and thanks to Tom that made me know \{-} , "\zs" and "\ze", I figured
out the expression

    s/".\+:\zs[^"]\{-}@.\{-}\ze"/[hidden email]

I just added the .\+: not to select the mailto: part and instead to
use an "all caracter" non-greedy, I used a "not a double-quote
character" non-greedy to select the first part of the email.
To use the same for the second part was superfluous since the last
double quote would stop the search, being the following characters non-
greedy themselves.

Thank you all again, I learned so much today. :)

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Vim RE and lazy operator

caruso_g
A little follow up.
Is there a way to highlight the matched string while constructing the
RE?
I mean, I set the option to show that while I make a search with /
or ?
But is there a similar option that I can set to do the same while I
begin to write the RE in s/ ?
If not, can I re-use the previous search in the s/ ex command with a
shortcut or something like that (I remember that I read it but I don't
remember where… :P ).
Thank you again.

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Vim RE and lazy operator

Jürgen Krämer-4

Hi

caruso_g wrote:

> A little follow up.
> Is there a way to highlight the matched string while constructing the
> RE?
> I mean, I set the option to show that while I make a search with /
> or ?
> But is there a similar option that I can set to do the same while I
> begin to write the RE in s/ ?
> If not, can I re-use the previous search in the s/ ex command with a
> shortcut or something like that (I remember that I read it but I don't
> remember where… :P ).

if you keep the pattern of a :s empty, i.e.,

  :%s//your replacement string/

the last used search pattern will get re-used.

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us.     (Calvin)

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Vim RE and lazy operator

caruso_g
> if you keep the pattern of a :s empty, i.e.,
>
>   :%s//your replacement string/
>
> the last used search pattern will get re-used.

Great, thanks Jürgen, it worked perfectly. :)

--
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