0

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?

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, that's a clever and concise solution. Also, thanks for adding the reason. These regexps are starting to make more sense to me now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.