The problem is i want to comment out a specific string in some files.

In most of them it looks like this

countries = {"GER","NZG",}

Replacing this is trivial. However there are some exception in which the string looks like this:

    countries = {

I replaced the first line in the first run through by replacing "countries" with "--countries".

Now I found the next row with


and Replaced it by

-- \1

Now my files look like this

 --countries = {
 --      "GER","NZG",

I know this is not very efficent.

As you can see, I need to comment out the last "}". It is the last "}" in every file. But there are other single "}" in one rows.

These are lua files so maybe block commenting would be better. Still i don't know how to find the last "}" in the file.

Would be nice if someone could help me with that.

  • 1
    Why is this tagged for microsoft-excel? – Robert Paulsen May 1 '19 at 16:16
  • Sorry, i just added the recommended Tags. I thought this is more of a RegEx problem so ppl who know it from other programms might know an answare. – Binary May 1 '19 at 16:43
  • Ctrl+H
  • Find what: ^(\w+\h*=\h*\{)([^}]+)(\})
  • Replace with: --$1\n--\t$2\n--$3
  • check Wrap around
  • check Regular expression
  • Replace all


^               # begining of line
  (             # start group 1
    \w+         # 1 or more word character
    \h*         # 0 or more horizontal spaces
    =           # equal sign
    \h*         # 0 or more horizontal spaces
    \{          # opening curly brace
  )             # end group 1
  (             # start group 2
    [^}]+       # 1 or more non closing brace
  )             # end group 2
  (\})          # group 3, a closing curly brace


--$1            # 2 hyphens and content of group 1
\n              # linefeed, you may use \r\n for Windows
--\t$2          # 2 hyphens, a tabulation, content of group 3
\n              # linefeed
--$3            # 2 hyphens and content of group 3

Result for given example:

--countries = {
--  "GER","NZG",

Screen capture:

enter image description here

  • Wow very nice. Okay this worked semi. It also found other strings which i solved by adding \bcountries\b to the first group instead of \w+. I also changed the replace with --[[ $1 \n \t$2 \n $3 ]] (for block commenting) Also thank you for the explanation what all those expressions mean. My last try to find those strings looked like this xD ^([\s]?\bcountries\b[\s][\=][\s][\{][\s]?){1} ([\s]?([\"][A-Z]{3}[\"])+[\s]?[\,]*)+([\s]?[\}]) – Binary May 1 '19 at 17:19
  • @Binary: You don't need to use character class for a single character, [\s] is better written \s, [\"] ==> ", [\=] ==> =, {1} is superfluous as \b between \s and countries If you want to only match countries, use (\bcountries\h*=\h*\{)([^}]+)(\}) – Toto May 1 '19 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.