4

In Atom, I'm working with a huge text file and I have to replace any occurrences of

.env(bar)

with

.env('bar')

where bar can have several values, with the standard Find-Replace tool.

I successfully managed to select (Find field) the patterns I want to change thanks to

.env(\(([^\)]+)\))

as reported here.

However I cannot find any proper regex to fill into the Replace field in order to wrap between quotes, e.g. none of these work:

.env(\'\(([^\)]+)\)\')
.env('\(([^\)]+)\)')

Moreover, how can I revert the whole process (also using Find-Replace regex)?

Thanks!

AlessioX
  • 3,167
  • 6
  • 24
  • 40
  • 1
    In replace field you can't use regular expressions but replacement strings such as referencing to captured groups. Find `\.env\(([^)]+)\)`, Replace with `.env('$1')` – revo Sep 05 '17 at 18:49
  • You try get the `.env(bar)` and replace for `.env('bar')`, rigth ? In this moment you can find the `.env(bar)` ? but cant replace with quotes the value of `bar`? Is this correct ? – kip Sep 05 '17 at 18:52
  • You cant get the capture group with $1, check the @revo comment.. – kip Sep 05 '17 at 18:52
  • @revo bullseye. Also finding `\.env\(\'([^)]+)\'\)` and replacing `.env($1)` reverts the whole thing. If you're willing to properly answer the question, I'll be very happy to accept it. – AlessioX Sep 05 '17 at 18:53

1 Answers1

6

While a pattern matches you can't fill replacement field with Regular Expressions because simply it doesn't work that way. You can't use Regular Expressions, but instead, replacement strings such as referencing to recent captured groups are available.

  1. Find: \.env\(([^)]+)\)
  2. Replace with: .env('$1')

$1 means capturing group #1

revo
  • 47,783
  • 14
  • 74
  • 117