0

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 = { "GER","NZG", } 

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

Now I found the next row with

^([\s][\"][A-Z]{3}[\"]) 

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.

2
  • 1
    Why is this tagged for microsoft-excel? Commented May 1, 2019 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. Commented May 1, 2019 at 16:43

1 Answer 1

0
  • Ctrl+H
  • Find what: ^(\w+\h*=\h*\{)([^}]+)(\})
  • Replace with: --$1\n--\t$2\n--$3
  • check Wrap around
  • check Regular expression
  • Replace all

Explanation:

^ # 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 

Replacement:

--$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

2
  • 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]?[\}]) Commented May 1, 2019 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*\{)([^}]+)(\}) Commented May 1, 2019 at 18:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.