Search code examples
regexreplacewshemeditor

Replace all occurrences of a character on every line excepting commented lines


I need a way to replace all characters "y" and "r" with "n" on every line in a file EXCEPT lines starting with ">" using EmEditor's built-in Regex++ (version 1.57 by Dr John Maddock). This regex flavor supports only fixed length lookbehind and also doesn't have all advanced .NET enhancements for regexes.

Here is an example input file:

> A header containing "y" and "r"    
tttttrtagggaar-rrgatctg--gcctrtcc---cacyaayygggayyyaggc

And here is desired result of replacement:

> A header containing "y" and "r"  
tttttntagggaan-nngatctg--gcctntcc---cacnaanngggannnaggc

It would be ideal to be able to perform such replacement in a single step. But currently I'm completely stuck at finding even a two-step or script-based solution (EmEditor allows writing Windows Scripting Host macroses). I know that I can find all non-commented lines with regex ^(?!>).*$, but haven't found a way to select them in order to perform the replacement r|yn in the selection only (using the corresponding option in the "Replace" dialog).

Is there a way to achieve this with EmEditor?


Solution

  • Since EmEditor default regex engine is Boost, you can do this:

    Find: ^>.*|([yr])

    Replace with: (?1:n:$0)

    Note: multi-line mode should be on.