Search code examples
regexnotepad++multiline

Notepad++ multi-line regex replacement only replacing first line


I want to comment out a large number of multi-line logging statements with a single regex replace all operation.

My [potentially] multi-line statements look like this:

logger.info( "Log Message: [" + myVariable 
    + "] is has a value of: [" + myVariable.getValue() + " ]"
    + LogColors.ANSI_RESET );

The following regex statement (with . matches newline enabled) successfully finds (and selects) all appropriate lines until the ending semicolon.

(logger\..+?\);)

However when I try to replace all with:

// \1

Only the first line of my tagged enclosed expression () is replaced. So I am left with this compiler unfriendly multiline statement:

// logger.info( "Log Message: [" + myVariable 
    + "] is has a value of: [" + myVariable.getValue() + " ]"
    + LogColors.ANSI_RESET );

Solution

  • Instead of trying to force everything into a single-line comment, try making it multiline. Replace it with /* \1 */. Your regex is the same, but it eliminates the problem of compilers hating it.

    This also has the advantage of keeping the code identical, so that when you uncomment it you're not losing formatting (which often makes the code easier to read).

    Note: If this isn't Java, replace /* */ with the relevant multiline.