How to concatenate a string in regexprep to include a variable and a string as the regular expression
Afficher commentaires plus anciens
I am trying to do a number of things in regexprep. I solve a number of cascading differential equations and then I want to quickly write them in latex. This will undoubtebly get bigger and so good programming is requried, hence why I pass the buck on....
I first want to read in the file and decide which 'order' of differnetial equations I want to computer. In this example I pick order 5 and so it goes that order differential equation in the file and strips the differential eqaution by the square brackets.
Then I want to return A5(1) to be A_{\mathrm{i}} in a text file BUT if it were the fourth order I wanted to do I would want A4(1) to be A_{\mathrm{i}} and so I want to concatenate ['A',nstr,'(1)'] to be A_{\mathrm{i}} where nstr is the nth string of the differential equation order.
I then want to also replace the kps string with k_{\mathrm{p+s}} but I can't get the plus sign to work out at all. I believe this is because plus is some kind of operator even though I stick it in the string. I have tried using '\d+', '\+' and it did not work. I still get some funny business.
Here is my MW (my code) you will need to locate the attached Matlab script.txt file on your own PC. help is much appreciated.
input = fileread('........\Matlab script.txt')
n=5
nstr = num2str(n)
input_str = extractBetween(input,['@(x,A',nstr',')['],']');
Multiplication_delete = regexprep(input_str,'*','');
ConjAmpswap = strrep(Multiplication_delete,{['conj(A',nstr,'(1))'],['conj(A',nstr,'(2))'],['conj(A',nstr,'(3))'],['conj(A',nstr,'(4))'],['conj(A',nstr,'(5))'],['conj(A',nstr,'(6))'],['conj(A',nstr,'(7))'],['conj(A',nstr,'(8))'],['conj(A',nstr,'(9))'],['conj(A',nstr,'(10))'],['conj(A',nstr,'(11))'],['conj(A',nstr,'(12))'],['conj(A',nstr,'(13))'],['conj(A',nstr,'(14))'],['conj(A',nstr,'(15))']},{'A_{\\mathrm{i}}^*','A_{\\mathrm{s}}^*','A_{\\mathrm{p}}^*','A_{\\mathrm{p\+i}}^*','A_{\\mathrm{p\+s}}^*','A_{\\mathrm{2p}}^*','A_{\\mathrm{2p\+i}}^*','A_{\\mathrm{2p\+s}}^*','A_{\\mathrm{3p}}^*','A_{\\mathrm{3p\+i}}^*','A_{\\mathrm{3p\+s}}^*','A_{\\mathrm{4p}}^*','A_{\\mathrm{4p\+i}}^*','A_{\\mathrm{4p\+s}}^*','A_{\\mathrm{5p}}^*'});
Ampswap = regexprep(ConjAmpswap,{['A',nstr,'(1)'],['A',nstr,'(2)'],['A',nstr,'(3)'],['A',nstr,'(4)'],['A',nstr,'(5)'],['A',nstr,'(6)'],['A',nstr,'(7)'],['A',nstr,'(8)'],['A',nstr,'(9)'],['A',nstr,'(10)'],['A',nstr,'(11)'],['A',nstr,'(12)'],['A',nstr,'(13)'],['A',nstr,'(14)'],['A',nstr,'(15)']},{'A_{\\mathrm{i}}','A_{\\mathrm{s}}','A_{\\mathrm{p}}','A_{\\mathrm{p\+i}}','A_{\\mathrm{p\+s}}','A_{\\mathrm{2p}}','A_{\\mathrm{2p\+i}}','A_{\\mathrm{2p\+s}}','A_{\\mathrm{3p}}','A_{\\mathrm{3p\+i}}','A_{\\mathrm{3p\+s}}','A_{\\mathrm{4p}}','A_{\\mathrm{4p\+i}}','A_{\\mathrm{4p\+s}}','A_{\\mathrm{5p}}'});
kswap = regexprep(Ampswap,{'ki','ks','kp','kpi','kps','k2p','k2pi','k2ps','k3p','k3pi','k3ps','k4p','k4pi','k4ps','k5p'},{'k_{\\mathrm{i}}','k_{\\mathrm{s}}','k_{\\mathrm{p}}','k_{\\mathrm{p\+i}}','k_{\\mathrm{p\+s}}','k_{\\mathrm{2p}}','k_{\\mathrm{2p\+i}}','k_{\\mathrm{2p\+s}}','k_{\\mathrm{3p}}','k_{\\mathrm{3p\+i}}','k_{\\mathrm{3p\+s}}','k_{\\mathrm{4p}}','k_{\\mathrm{4p\+i}}','k_{\\mathrm{4p\+s}}','k_{\\mathrm{5p}}'});
exponentopen = regexprep(kswap,{'exp('},{'e^{'});
exponentclose = regexprep(exponentopen,{'x)'},{'x}'});
beta_factor = regexprep(exponentclose,{'(maxBeta/2)'},{'\\Big(\\dfrac{\\beta}{2}\\Big)'});
i_replace = regexprep(beta_factor,{'1i'},{'i'});
10 commentaires
"I then want to also replace the kps string with k_{\mathrm{p+s}} but I can't get the plus sign to work..."
>> str = 'hello kps world';
>> regexprep(str,'kps','k_\{\\mathrm\{p\+s\}\}')
ans =
hello k_{\mathrm{p+s}} world
strrep would probably be easier to use, as you can avoid the significant overlap between LateX and regular expression active characters (which need escaping):
>> strrep(str,'kps','k_{\mathrm{p+s}}')
ans =
hello k_{\mathrm{p+s}} world
Thomas Dixon
le 25 Nov 2019
Modifié(e) : Thomas Dixon
le 25 Nov 2019
You might also be able to find a function to help you:
Note that things like replacing exp(...) with e^{...} could be done in one step with regexprep and dynamic regular expressions (as long as there is no nesting of parentheses, in which case you need a language parser):
>> str = 'hello exp(2*blah) world';
>> regexprep(str,'exp\(([^\)]+)\)','e^{$1}')
ans =
hello e^{2*blah} world
Thomas Dixon
le 26 Nov 2019
"Do you think it is possible to use this functionality to map the syntax."
For that you need a language parser, which is not a trivial thing to write. Of course the best parser for the MATLAB language is MATLAB, third-party tools are unlikely to behave in the exactly the same way. AFAIK MATLAB does not offer documented access to its parser, other than calling the code.
You might be able to get one these to do most of what you want:
Thomas Dixon
le 26 Nov 2019
"But it doesn't seem to like this and the two first lines of code do nothing for strrep or regexprep."
You did not escape the parentheses in any of your regular expressions, so for example:
['A',nstr,'(1)']
e.g.: 'A5(1)'
will actually match
A51
because parentheses are grouping charcters, and are not literal characters to match. If you really want to match literal parentheses then you will need to escape them, e.g.:
['A',nstr,'\(1\)']
and repeatedly read the regular expression documentation very carefully:
Personally I would use dynamic regular expressions rather than those huge cell arrays of almost-identical match and replace character vectors, either with some simple indexing into a cell array or using an algorithm that converts from the (x) value to the \mathrm{...} content.
Thomas Dixon
le 26 Nov 2019
Perhaps something like this:
>> C = {'','\+i','\+s'};
>> F = @(n)sprintf('%dp%s',fix(n/3),C{1+mod(n,3)});
>> G = @(s)regexprep(F(sscanf(s,'%d')),{'^0p','^\\\+'},'');
>> rgx = sprintf('A%d%s',4,'\((\d+)\)'); % specify the "n" value here
>> rpl = 'A_\{\\mathrm\{${G($1)}\}\}';
>> regexprep('hello A4(1) world',rgx,rpl)
ans =
hello A_{\mathrm{i}} world
>> regexprep('hello A4(2) world',rgx,rpl)
ans =
hello A_{\mathrm{s}} world
>> regexprep('hello A4(3) world',rgx,rpl)
ans =
hello A_{\mathrm{1p}} world
>> regexprep('hello A4(4) world',rgx,rpl)
ans =
hello A_{\mathrm{1p\+i}} world
>> regexprep('hello A4(5) world',rgx,rpl)
ans =
hello A_{\mathrm{1p\+s}} world
>> regexprep('hello A4(6) world',rgx,rpl)
ans =
hello A_{\mathrm{2p}} world
>> regexprep('hello A4(7) world',rgx,rpl)
ans =
hello A_{\mathrm{2p\+i}} world
You might like to download my FEX submission:
it lets you quickly try regular expressions and see regexp's outputs as you type.
Thomas Dixon
le 26 Nov 2019
Réponses (0)
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!