Home > OS >  Replace all lines that start with #WAV with one single line, notepad
Replace all lines that start with #WAV with one single line, notepad

Time:09-21

I have thousands of files which all have a single chunk of lines that start with #WAV. This chunk could come anywhere in the file but is always a "chunk"

#WAV 001.wav
#WAV "something"
#WAV 21021029910291029.ogg

These chunks could be 1000 lines long or just 3 like the example above

I need to replace the entire chunk (all the lines that start with #WAV) with one single line.

So the above would just become

EXAMPLE REPLACEMENT

Note that this needs to work for any size chunk and what could come after #WAV is unpredictable.

using #WAV.* as regex wont work because it will replace all occurances of that line and not the entire chunk.

using #WAV(.*)#WAV and trying to replace the capture group wont work because I cant be sure of the "end" of the capture group.

CodePudding user response:

Use

^#WAV\b.*(?:\R#WAV\b.*)*

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  #WAV                     '#WAV'
--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \R                       line ending 
--------------------------------------------------------------------------------
    #WAV                     '#WAV'
--------------------------------------------------------------------------------
    \b                       the boundary between a word char (\w)
                             and something that is not a word char
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )*                       end of grouping

CodePudding user response:

You can use the following RegEx:

(^#WAV.*[\r\n] ) 

This will match all lines starting with #WAV on a single selection.

  • Related