Reputation: 867
I want to use regex to match markdown titles which start with 1-6 #
marks, however, in the markdown file, there are python code snippets with comments start with #
, which could be considered as markdown title wrongly.
Could you find a regex which can parse markdown title corrently? for example I have below markdown snippets:
# This is a title
## This is also a title
```python
import os
# this is a comment, not a title
os.system("pause")
```
Upvotes: 0
Views: 269
Reputation: 378
Another general way to do that is:
:%s/\(```python\_.\{-}```\)\|^\(\W\+\)\@<!/**\1
Then:
:%s/^\#\(.*\)/"\1"
Finally:
%s/^\**//g
Example, before:
# This is a title
## This is also a title
```python
import os
# this is a comment, not a title os.system("pause")
```
# This is a title
## This is also a title
```python
import os
# this is a comment, not a title os.system("pause")
print("Blah blah")
# this is another comment
```
After:
# This is a title
## This is also a title
```python
import os
" this is a comment, not a title os.system("pause") "
```
# This is a title
## This is also a title
```python
import os
" this is a comment, not a title os.system("pause")"
print("Blah blah")
" this is another comment "
```
Another simpler way is:
:%s/\(```\_.\{-}```\n\)\zs\|#\+\( .*\)/"\2 "/g
Upvotes: 1
Reputation: 378
If I understand, this will help you:
:%s/^\(\(```python\(.*\n\(^```.*$\)\@!\)\+.*#\)\)/\1**/g
then:
:%s/\#\*\*\(.*$\)/"""\1"""/g
First one will change the #
character inside every python snippet to #**
at the snipped python code.
Last one will change #**
to a more python general format """ comment """
LIMITATIONS: only works for only one comment by snippet.
Upvotes: 2