Home > Net >  notepad convert \section{text} to # text
notepad convert \section{text} to # text

Time:06-08

I have some tex files with \section{text} and \subsection{text}, etc. And I want to convert them to # text and ## text in markdown files using regular expressions in Notepad . How can I achieve that?

CodePudding user response:

Open the replace window with Ctrl H. Check the radio button "Regular Expression" and search for:

\\section\{([^}]*)}

And replace with:

# \1

For subsections:

\\subsection\{([^}]*)}
## \1

What we're doing:

  • \\ is an escaped backslash matching the litteral backslash of your expression
  • { needs to be escaped as well otherwise it would be recognized as quantifier, hence \{
  • ([^}]*) is a group made of 0 or more characters that are NOT }
  • \1 is a reference to the first and only group of our regular expression

CodePudding user response:

It can be done in a single pass:

  • Ctrl H
  • Find what: \\(sub)?section{([^}]*)}
  • Replace with: (?1#)# $2
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

\\          # a backslash, have to be escaped
(sub)?      # group 1, literally "sub", optional
section{    # literally
([^}]*)     # group 2, 0 or more any character that is not "}"
}           # "}" character

Replacement:

(?1#)       # conditional replace, if group 1 exists, print a "#"
# $2        # "#", a space and content of group 2

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

  • Related