regexnotepad++regexp-replace

How do I replace the closing bracket also?


I have a couple of sentences in parentheses in a couple of files in a folder and I need to find and remove the opening bracket and replace the closing bracket of every parenthesis with a hyphen/dash (-). I don't know if Notepad++ is the best to use for this but I opened one of those files with Notepad++, selected the Regular Expression mode and put this in the "Find" field:-

(<!--.*?-->|<style[\S\s\n]*?</style>|<script[\S\s\n]*?</script>|rgb\(\d+,\x20\d+,\x20\d+\))(*SKIP)(*F)|\((.+?\))\s*again

and

$1 - again

in the Replace in files field to replace (Mal&eacute;) again with Mal&eacute; - again but when I hit Replace, the closing bracket is not removed. Please give me the correct Regular Expression to replace the parentheses. This is part of one of the files that can be used as a sample for testing the above RegEx:-

<!-- (Malé) -->
<p>(Mal&eacute;) again</p>
<p style="font-family: &quot;verdana&quot;; font-size: 18px; color: rgb(0, 0, 0);">Mal&eacute; again</p>
<style>
code {
  font-family: &quot;verdana&quot;;
  font-size: 18px;
  color: black;
  font-weight: bold !important;
  line-height: 1.6 !important;
  }
@media (min-width: 1281px) {
code {
  font: 24px "verdana" bold !important;
  }
}
</style>
<script type="text/javascript">
function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
}
</script>

Solution

  • Goal

    Removing the opening bracket and replacing the closing bracket with -

    Find

    (?:<!--.*?-->|<style(?s:.*?)</style>|<script(?s:.*?)</script>|rgb\(\d+,\x20\d+,\x20\d+\))(*SKIP)(*F)|\(([^()]*)\)\s*again
    

    Replace

    $1 - again
    

    Input

    <!-- (Malé) -->
    <p>(Mal&eacute;) again</p>
    <p style="font-family: &quot;verdana&quot;; font-size: 18px; color: rgb(0, 0, 0);">Mal&eacute; again</p>
    <style>
    code {
      font-family: &quot;verdana&quot;;
      font-size: 18px;
      color: black;
      font-weight: bold !important;
      line-height: 1.6 !important;
      }
    @media (min-width: 1281px) {
    code {
      font: 24px "verdana" bold !important;
      }
    }
    </style>
    <script type="text/javascript">
    function googleTranslateElementInit() {
      new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
    }
    </script>
    

    Output

    <!-- (Malé) -->
    <p>Mal&eacute; - again</p>
    <p style="font-family: &quot;verdana&quot;; font-size: 18px; color: rgb(0, 0, 0);">Mal&eacute; again</p>
    <style>
    code {
      font-family: &quot;verdana&quot;;
      font-size: 18px;
      color: black;
      font-weight: bold !important;
      line-height: 1.6 !important;
      }
    @media (min-width: 1281px) {
    code {
      font: 24px "verdana" bold !important;
      }
    }
    </style>
    <script type="text/javascript">
    function googleTranslateElementInit() {
      new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
    }
    </script>