How to use regex to indent code in notepad++

前端 未结 2 713
再見小時候
再見小時候 2021-01-26 01:45

for example I have the following code

Module MPI
    Use MPI
    !
! MPI info
    If (true) Then
        Print *, \'\'
! empty line 1
! empty line 2
    End If
          


        
2条回答
  •  长情又很酷
    2021-01-26 01:54

    Using the search text ^( +)(.*\R)(!) and the replace text \1\2\1\3 then clicking on "Replace all" twice does what is wanted on the sample text. I cannot see a way of doing this in one pass.

    The expression looks for a line with leading spaces followed by a line starting with a !. The capture groups are the leading spaces in \1, the rest of that line including the newline in \2 and the leading ! in \3. The replacement just assembles the captures in the right order. Note that you could omit the capture group around the ! and just have an explicit `! in the replacement, but I like to use captures in such contexts as they often allow for shorter replacements (although not in this case) and easier enhancements.

提交回复
热议问题