Home > Software engineering >  VSCode - find and replace with regexp, but keep word in the middle
VSCode - find and replace with regexp, but keep word in the middle

Time:07-11

I have multiple occurrences of

AppLocalizations.of(context).translate('WORDING_STR')

in my code, that I would like to change to

LocaleKeys.WORDING_STR.tr()

Is it possible to do it with regular expressions, so I can keep whatever is in the code as WORDING_STR?

example:

Replace AppLocalizations.of(context).translate('finish_status') to LocaleKeys.finish_status.tr()

CodePudding user response:

In VSCode, try this regex to find the occurrences:

AppLocalizations\.of\(context\)\.translate\('(\w )'\)

And this one to replace:

LocaleKeys.$1.tr()

Explanation

\w means match a word character only. And because it's wrapped around parens it creates a capturing group that we can back-reference with $1 to use the captured word in the first group to replace the text. Check it out on Screenshot before Screenshot after

  • Related