在for循环中用regexprep替换字符串? (MATLAB) [英] Replacing strings with regexprep inside a for loop? (MATLAB)

查看:415
本文介绍了在for循环中用regexprep替换字符串? (MATLAB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Matlab中,我在程序化查找和替换方面遇到了很多麻烦.我在这里转载了MWE.

I've been having a lot of trouble with programmatic find and replace, in Matlab. I have reproduced a MWE here.

说我在名为"BaseJac3.txt"的文本文件中有以下代码:

Say I have the following code in a textfile called 'BaseJac3.txt':

x2 = -S1_2*(r1+a*K+a*P)+k*KS1_1+d*(PS1_1+KS1_2);
x3 = -S1_3*(r1+a*K+a*P)+k*KS1_2+d*(PS1_2+KS1_3);
x4 = -S1_4*(r1+a*K+a*P)+k*KS1_3+d*(PS1_3+KS1_4);
x5 = -S1_5*(r2+a*K+a*P)+k*KS1_4+d*(PS1_4+KS1_5);

我需要更换

S1_2y(2),...,S1_5y(5);

KS1_1KS1_5,以及y(52)y(57)

PS1_1PS1_4,以及y(102)y(105)

我该怎么做?我试过使用此工具: http://www. mathworks.com/matlabcentral/fileexchange/42877-find-and-replace-in-files

How can I do this? I've tried using this: http://www.mathworks.com/matlabcentral/fileexchange/42877-find-and-replace-in-files

还有我的以下代码:

text1 = fileread('BaseJac3.txt');

for k = 1:4
    regexprep(text1,['PS1_' num2str(k)],['y(' num2str(k+101) ')']);
end
for k = 1:5
    regexprep(text1,['KS1_' num2str(k)],['y(' num2str(k+51) ')']);
end
for k = 2:5
    regexprep(text1,['S1_' num2str(k)],['y(' num2str(k) ')']);
end

,但似乎都无法正常工作.我不确定正则表达式.

but neither seems to be working properly. I'm not sure about regular expressions.

预先感谢您的帮助.

推荐答案

某些正则表达式专家可以一次完成所有这些工作,但为清楚起见,我将其分为3个阶段. regexprep 适用于字符串单元格数组,因此您可以将其传递给一次完成整个阵列.

Some regex whiz can probably work up a way to do this all in one shot, but for clarity I've split it into 3 passes. regexprep works on cell arrays of strings so you can pass it the entire array at once.

function newstr = testcode(str)
helper = @(x,y) num2str(str2double(x) + y);  % Generate anonymous function for our dynamic regexrep expression

pass1 = regexprep(str, '(?<!(K|P))S1_(\d*)', 'y($1)');  %S1
pass2 = regexprep(pass1, 'KS1_(\d*)', 'y(${helper($1,51)})');  % KS1
pass3 = regexprep(pass2, 'PS1_(\d*)', 'y(${helper($1,101)})');  % PS1
newstr = pass3;
end

我在这里所做的是利用regexprep令牌动态表达式进行格式化输出.

What I've done here is utilize regexprep's Tokens and Dynamic Expressions to format the output.

第1阶段适用于S1,并使用在断言后面查找忽略KS1PS1.该表达式与S1_之后的数字匹配,并将其用作y()的标记.

Pass 1 works on S1, and uses a lookbehind assertion to ignore KS1 and PS1. The expression matches the digit following S1_ and uses it as the token for y().

第2和第3遍在KS1PS1上起作用.它们使用相同的令牌方法进行匹配,但也使用动态表达式进行替换.这些使我们可以将令牌传递给任何返回字符串的MATLAB函数,并使用该字符串作为替换.我已经定义了一个辅助匿名函数helper来照顾所需的索引偏移量.

Passes 2 and 3 work on KS1 and PS1. These use the same token approach for the match but also use a dynamic expression for the replacement. These allow us to pass a token to any MATLAB function that returns a string, and uses that string as the replacement. I've defined a helper anonymous function, helper, to take care of the desired index offset.

使用以下输入:

str = {'x2 = -S1_2*(r1+a*K+a*P)+k*KS1_1+d*(PS1_1+KS1_2);'; ...
       'x3 = -S1_3*(r1+a*K+a*P)+k*KS1_2+d*(PS1_2+KS1_3);'; ...
       'x4 = -S1_4*(r1+a*K+a*P)+k*KS1_3+d*(PS1_3+KS1_4);'; ...
       'x5 = -S1_5*(r2+a*K+a*P)+k*KS1_4+d*(PS1_4+KS1_5);' ...
       };

我们得到以下输出:

>> A = testcode(str)

A = 

    'x2 = -y(2)*(r1+a*K+a*P)+k*y(52)+d*(y(102)+y(53));'
    'x3 = -y(3)*(r1+a*K+a*P)+k*y(53)+d*(y(103)+y(54));'
    'x4 = -y(4)*(r1+a*K+a*P)+k*y(54)+d*(y(104)+y(55));'
    'x5 = -y(5)*(r2+a*K+a*P)+k*y(55)+d*(y(105)+y(56));'

这篇关于在for循环中用regexprep替换字符串? (MATLAB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆