0

I would need to replace the strings contained within the curved brackets with the same strings but with an initial prefix and curly brackets \fill{(test_string)}. Is this possible?
Example:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore.
(first_string)
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
nibh euismod tincidunt ut laoreet dolore.
(second_string)
 

Transform in:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam
nonummy nibh euismod tincidunt ut laoreet dolore.
\fill{(first_string)}
Lorem ipsum dolor sit amet, consectetuer
adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore.
\fill{(second_string)}
rioV8
  • 24,506
  • 3
  • 32
  • 49
Andy Toff
  • 51
  • 4
  • learn regular expression search replace – rioV8 Nov 11 '22 at 12:22
  • Thank you for your reply! I am interested in understanding and learning. Any useful links? – Andy Toff Nov 11 '22 at 13:06
  • read the MDN pages on Regex, they deal with the JavaScript dialect used by VSC, there are tons of webpages about regex, read SO posts with tag `regex` – rioV8 Nov 11 '22 at 15:23

1 Answers1

1

As suggested, you could use a regex search and replace. A simple example of this can be found here. In your case, this should work:

image of regex used to find the string to replace

The regular expression \(([^\)]+)\) does the following (as taken from this site - you'll need to paste the regex into the site to see the explanation):

  • \( matches the character literally (case sensitive)
  • 1st Capturing Group ([^\)]+)
    • Match a single character not present in the list below [^\)]
      • + matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
      • \) matches the character ) literally (case sensitive)
  • \) matches the character ) literally (case sensitive)

In Visual Studio Code, if you enable regex search by clicking the .* icon in the search bar, you can put this regular expression in. Then, in the replace section, you can put \fill{($1)} where the $1 is the 1st Capturing Group mentioned previously (the first_string, second_string, etc. part found by the regular expression).

There are a lot of Regex posts here on Stackoverflow you may want to read. One notable one is Greedy versus Lazy.

Timothy G.
  • 6,335
  • 7
  • 30
  • 46
  • 2
    You can use the simpler `(\(.+?\))` and replace with `\fill{$1}`. The greedy `?` simplifies it. And it is worth mentioning that nested `()` with a string is a big problem for a regex to solve. – Mark Nov 11 '22 at 16:36