0

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
    Integer ithread, nthread, ierr
End Module MPI

The lines start with ! sign which is comment lines in fortran. I want those comment lines have the same indent as their previous line indent.

That is I want this format

Module MPI
    Use MPI
    !
    ! MPI info
    If (true) Then
        Print *, ''
        ! empty line 1
        ! empty line 2
    End If
    Integer ithread, nthread, ierr
End Module MPI

I want to do this in notepad++ using regex. But if there are better choice feel free to answer.

Here is what I have tried: replace ^(\s*)(.*?\r\n)\s*\! as $1$2$1!. However it produce

Module MPI
    Use MPI
    !
! MPI info
    If (true) Then
        Print *, ''
        ! empty line 1
! empty line 2
    End If
    Integer ithread, nthread, ierr
End Module MPI

There is still two lines not right. It seems that though the pattern ^(\s*)(.*?\r\n)\s*\! matches this line, however, it just skip it for the regex engine already matched previous lines.

My question is how to solve this indent problem with regex?

3
  • This looks like a job for a parser. Commented Jul 19, 2016 at 7:44
  • @TimBiegeleisen My question is actually a post processing of the result generated by universalindentGUI Commented Jul 19, 2016 at 8:00
  • It is allowed to use the same regex more times? For example, you have at most 3 consecutive comment lines, so you will click on the "replace all" button 3 times. Commented Jul 19, 2016 at 8:29

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Since the engine is already passed on a comment line to indent it, I think it is impossible to use the same entire edited line for the next match to get the number of spaces. So I think you have to repeat the same replacement more times. Try with:

^(\s*)([^!\s].*?\r\n(\1\!.*?\r\n)*)\s*\!

always replacing it with $1$2$1!.

Like I said in the comment, if you have at most N consecutive comment lines, you will click on the "replace all" button N times

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.