3

Converting odt to markdown produces \ around special characters. So I need to change \\ to \ and remove if \ is standalone.

Sample text:

* \[\] match any of the character specified within \[\], use \[^\] to invert the selection
* \\W opposite of \\w i.e short cut for \[^a-zA-Z0-9\_\]
* \\w match alphabets (both upper & lower cases), numbers and \_ i.e short cut for \[a-zA-Z0-9\_\]

Expected output:

* [] match any of the character specified within [], use [^] to invert the selection
* \W opposite of \w i.e short cut for [^a-zA-Z0-9_]
* \w match alphabets (both upper & lower cases), numbers and _ i.e short cut for [a-zA-Z0-9_]

On using :% s/\v([^\\])\\([^\\])/\1\2/g I get this:

* [\] match any of the character specified within [\], use [^] to invert the selection
* \\W opposite of \\w i.e short cut for [^a-zA-Z0-9_\]
* \\w match alphabets (both upper & lower cases), numbers and _ i.e short cut for [a-zA-Z0-9_\]

So I use the substitution again to take care of sequences like \[\] followed by :% s/\\\\/\\/g to get the desired final output. Is there is a simpler way to do this?

5

This should work:

:%s/\\\?\zs\\//g

Here the first \\\?\zs is used to ignore the first \ if there are two of them. See :h \? and :h \zs for more information.

2

Alternate solution (based on a comment I received for this question, but was deleted before I could reply)

:%s/\v(\\?)\\/\1/g

This is useful as I can now use sed also (couldn't find equivalent of \zs in sed):

sed -r 's/(\\?)\\/\1/g'

and \K seems equivalent of \zs in Perl (saw it while going through this answer)

s/\\?\K\\//g
1

I think you are over-complicating your original regex by trying to trap the character before the backslash. The following seems to work on your sample text. You will need to expand the [...] expression to include all special characters you may come across.

:%s!\v\\([][\_])!\1!g

[][\_] is a single entity comprising ] (which has to come first) and [,\,_ but you probably already knew that.

  • yeah, the one I posted in question was complicated and couldn't handle many cases.. yours does indeed work very well for sample text I gave, however it won't catch all cases, like \* \# etc.. thanks for different approach, something new to learn :) – Sundeep Jun 1 '16 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.