Grep search replace escaping question

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

Grep search replace escaping question

mstep.germany


Hello all!


I am pretty new to vim. I am pretty experimented in grep. So bare with me.

I need to replace all occurrences in a LaTeX-file of

\textcolor{LRed}{bar}

with:

bar

That means I need only to remove this exact LaTeX code around "bar". I don't know what to escape in my grep pattern. I tried nearly everything to escape : ( ) [ ] \ ...

But nothing seems to work. Is there somewhere a list of special characters, which I have to escape in a grep pattern?

My grep is as follows (without any escapes) :

:%s@\textcolor{LRed}{([^}]+)}@\1@g

What I have to escape here?


Thank you in advance


marek

--
--
You received this message from the "vim_mac" 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_mac" 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: Grep search replace escaping question

Andrew Stewart

> I need to replace all occurrences in a LaTeX-file of
>
> \textcolor{LRed}{bar}
>
> with:
>
> bar
>
> That means I need only to remove this exact LaTeX code around "bar". I don't know what to escape in my grep pattern. I tried nearly everything to escape : ( ) [ ] \ ...
>
> But nothing seems to work. Is there somewhere a list of special characters, which I have to escape in a grep pattern?

Yes, you can find it by typing `:help magic`.  "magic" is a setting which tells Vim which characters have special meaning in a regex.

One way to figure out the pattern is by trying to search for it (by typing `/pattern`) because it uses the same regex rules.


> My grep is as follows (without any escapes) :
>
> :%s@\textcolor{LRed}{([^}]+)}@\1@g
>
> What I have to escape here?

This should work:

%s@\\textcolor{LRed}{\(\w\+\)}@\1@g

Or this, which specifies a "very magic" pattern:

%s@\v\\textcolor\{LRed\}\{(\w+)\}@\1@g

Yours,
Andrew Stewart

--
--
You received this message from the "vim_mac" 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_mac" 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: Grep search replace escaping question

J. Scott Franko
Only the leading \ needs escaped because it is the escape character itself.  If it had been this slash:  “/“, would have had to escape that as well “\/“ to avoid conflict with the default delimiter for %s.  

And now that I think about it, I didn’t need g because that is for more than one occurrence of the same search string on one line.  In this can there is only one occurrence of the search string per line and that is the whole line.


:%s/\\textcolor{LRed}{bar}/bar/g

 

On Aug 15, 2018, at 7:31 AM, Andrew Stewart <[hidden email]> wrote:


I need to replace all occurrences in a LaTeX-file of

\textcolor{LRed}{bar}

with:

bar

That means I need only to remove this exact LaTeX code around "bar". I don't know what to escape in my grep pattern. I tried nearly everything to escape : ( ) [ ] \ ...

But nothing seems to work. Is there somewhere a list of special characters, which I have to escape in a grep pattern?

Yes, you can find it by typing `:help magic`.  "magic" is a setting which tells Vim which characters have special meaning in a regex.

One way to figure out the pattern is by trying to search for it (by typing `/pattern`) because it uses the same regex rules.


My grep is as follows (without any escapes) :

:%s@\textcolor{LRed}{([^}]+)}@\1@g

What I have to escape here?

This should work:

%s@\\textcolor{LRed}{\(\w\+\)}@\1@g

Or this, which specifies a "very magic" pattern:

%s@\v\\textcolor\{LRed\}\{(\w+)\}@\1@g

Yours,
Andrew Stewart

--
--
You received this message from the "vim_mac" 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_mac" 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_mac" 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_mac" 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: Grep search replace escaping question

mstep.germany
In reply to this post by Andrew Stewart

Thank you  Andrew Stewart and J. Scott Franko!


I will read :help magic

Thank you!


@Andrew

What I have to escape in a nagative construct like

{([^}]+)}

That means:

search for an opening curly { match all what is NOT a curly closing } and at the end an closing curly }

Thank you for your prompt answers.


marek

--
--
You received this message from the "vim_mac" 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_mac" 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: Grep search replace escaping question

Andrew Stewart
@Andrew

What I have to escape in a nagative construct like

{([^}]+)}


Just replace the \w with [^}]

So with default "magic":

{\([^}]\+\)}

With "very magic":

\{([^}]+)\}

Yours,
Andrew Stewart

--
--
You received this message from the "vim_mac" 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_mac" 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: Grep search replace escaping question

mstep.germany
On Wednesday, 15 August 2018 15:32:25 UTC+2, Andrew Stewart  wrote:

> @Andrew
>
> What I have to escape in a nagative construct like
>
> {([^}]+)}
>
>
>
> Just replace the \w with [^}]
>
>
> So with default "magic":
>
>
> {\([^}]\+\)}
>
>
> With "very magic":
>
>
> \{([^}]+)\}
>
>
> Yours,
> Andrew Stewart
Wow! You are very fast! Thank you! I still don't know, what you mean with "very magic". Yes yes, I will read :help magic

:-)

But it is looking more complicate your "magic"! Two escapes more!


marek

--
--
You received this message from the "vim_mac" 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_mac" 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: Grep search replace escaping question

Andrew Stewart

> But it is looking more complicate your "magic"! Two escapes more!

As far as I know the idea is that "very magic" is typical regex syntax, whereas "magic" is something invented for Vim so that patterns which match code require less escaping.

--
--
You received this message from the "vim_mac" 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_mac" 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.