Find and Replace with Regex


#1

First I’m sorry, I’ve been looking for this here and in others links but i didn’t get it yet.

I’m having difficulties to replace different strings but with the same pattern using an specific regex.

Follow some examples:

model="data.extra.profile.name"
model="data.age"
model=“data.more.some”

And here’s the change that I need:

model="{{ data.extra.profile.name }}“
model=”{{ data.age }}“
model=”{{ data.more.some }}"

So, I’m using the expression (\model="data.*?\") to find those strings, but i’m not understanding how replace without loose the middle value.

I saw something related using $1, maybe this could be the position if I use something like:

(\model="data.*?\")|(\model="{{data.*?\}}")

I would have $1 and $2? I don’t know.

Hope someone could explain that.


#2

You can use grouped parts of Your regex (those inside parenthesis) in replacing phase with ${group_number} expression. So I grouped only thing that varies in your example and used it in replacing, in order not to lose variable data.

Find: model="data([^"]*)"
Replace: model="{{ data$1 }}"


#3

lol, have just tried something like that.

Find: ^model="data(.*?)"
Replace: model="{{ data$1 }}"

It seems worked.

Anyway thanks for your time.