ohnoplus
ohnoplus

Reputation: 1325

can't get specific query-replace-regexp in emacs to work

I have a block of code as such, and I am trying to modify it with query-replace-regexp in emacs.

fz.D(1,:) = Dcen;                
fz.vol(1,:) = (4/3)*pi*((fz.D(1,:)/2).^3);          
fz.POC(1,:) = Ac*fz.vol(1,:).^bc;         
fz.w(1,:) = cw*fz.D(1,:).^eta;

% size - bin edges
fzl.D(1,:) = Dlim;     

I want it to look as so:

fz.D(ind,:) = fz.D(1,:);                
fz.vol(ind,:) = fz.vol(1,:);          
fz.POC(ind,:) = fz.POC(ind,:);   

and so fourth.

I tried to enact this change with the following, but it doesn't seem to work

query-replace-regexp \(*\)(1,:) = *; -> \1(k,:) = \1(1,:);

But that seems to do nothing.

Any suggestions about how I should fix this?

Upvotes: 0

Views: 203

Answers (1)

J Earls
J Earls

Reputation: 1812

I don't know emacs, but your regular expression needs to use .* for the 'match any length substring' operation:

query-replace-regexp \(.*\)\((1,:)\) = .*; -> \1(ind,:) = \1\2;

(This also makes use of a second \(\) group to avoid repeating the part of the pattern that you want to repeat in the replacement text)

The reason:

In regular expressions, * is a postfix operator that matches "0 or more of the previous item". When found with no previous item, it matches a plain *. Thus, your expression \(*\)(1,:) = *; matched the exact text *(1,:) = followed by 0 or more spaces followed by ;. You want to use .* to "match anything", as this matches 0 or more . items (where . matches any one non-end-of-line character).

Upvotes: 2

Related Questions