ohnoplus
ohnoplus

Reputation: 1325

emacs copy text after symbol in each line

I have a block of text that looks something like

par.dm_std;
par.dm_POM;
par.dm_CaCO3;

and I want it to look like

par.dm_std = dm_std;
par.dm_POM = dm_std;
par.dm_CaCO3 = dm_CaCO3;

So I am essentially trying to copy everything after the "." and put an equals sign before and a semicolon afterward. I tried to run a query replace with

par\.\(.*\) -> par\.\1 = \1;

but then emacs returns the error message

Invalid use of `\' in replacement text

I can't figure out for the life of me what I am doing wrong here? By the way, this is matlab code I am working with.

Upvotes: 1

Views: 56

Answers (2)

Barmar
Barmar

Reputation: 781761

You should not escape . in the replacement text. You also should have a literal ; at the end of the match expression; otherwise, it will be included in \1 and you'll get an extra semicolon before the equal sign.

Replace regexp: par\.\(.*\);
Replace with: par.\1 = \1;

Upvotes: 2

ohnoplus
ohnoplus

Reputation: 1325

Apparently, I should have used replace-regex rather than query-replace-regex. With the former, everything just works.

Upvotes: 0

Related Questions