Extracting values from string using regexp
61 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a string array that contains a list of equations. I would like to read these equations and use it for calculation, but I wasn't able to find a good way to do it that doesn't involve 'eval' function (for efficiency). Instead, I'd like to extract values from it using 'regexp' and re-construct the equation. But I am struggling with how to set up regexp.
Here are example equations:
k1 = "6.8e-9*Te^0.67*exp(-4.4/(Te+0.5))"
k2 = "6.8e-9*exp(-4/Te)"
k2 = "1.2e7"
These equations follow a general form of A * Te^n * exp(B/(Te+C)). I would like to extract the value of A, n, B, and C and store it in a matrix like [A, n, B, C]. So in this case, I would like to have the following as a result
k_value = [6.8e-9, 0.67, -4.4, 0.5; 6.8e-9, 0, -4, 0; 1.2e7, 0, 0, 0]
Once I have these values as a matrix, I can evaluate the original equation (given the value of Te) like
k = k_value(:,1) .* Te.^(k_value(:,2)) .* exp(k_value(:,3) / (Te + k_value(:,4)))
How can I use 'regexp' (or other method) to construct 'k_value' as above?
Thank you for your time!
1 个评论
Stephen23
2020-11-5
Extracting just the numbers is easy and efficient using regular expressions:
str = {'6.8e-9*Te^0.67*exp(-4.4/(Te+0.5))','6.8e-9*exp(-4/Te)','1.2e7'};
rgx = '[-+]?\d+\.?\d*([eE][-+]?\d+)?';
out = regexp(str,rgx,'match');
out{:}
The hard part is knowing which part of the expression they come from, which the accepted answer does not do.
采纳的回答
Cris LaPierre
2020-11-4
I'm not sure how to extract zero values for patterns that don't appear. I did want to point out that MATLAB has introduced new pattern matching capabilities in R2020b (see this blog post). This may help you, particularly if you, like me, can't make heads or tails of regular expressions.
It still takes some getting used to, but I was able to stitch together the following. Mind you, the outputs are still strings, but that's easy enough to handle (str2double). Since i couldn't think of a good way to automate filling in your coefficients matrix, I didn't attempt that.
k1 = "6.8e-9*Te^0.67*exp(-4.4/(Te+0.5))";
k2 = "6.8e-9*exp(-4/Te)";
k3 = "1.2e7";
pat = digitBoundary("start") + wildcardPattern(1,inf) + ...
lookAheadBoundary("*"|"/"|")"|textBoundary("end"));
extract(k1,pat)
extract(k2,pat)
extract(k3,pat)
4 个评论
Stephen23
2020-11-5
编辑:Stephen23
2020-11-5
So far this does answer does not provide the final (and most important) step that Tae Lim asked for. Can someone show me how to generate the requested output matrix k_value automatically from this answer? Requested matrix:
k_value = [6.8e-9, 0.67, -4.4, 0.5; 6.8e-9, 0, -4, 0; 1.2e7, 0, 0, 0]
This answer returns strings with no indications of which part of the expression they come from, so it is unlcear to me how this can be used to automatically generate the requested output matrix k_value.
@Cris LaPierre : can you please complete the answer? I am curious how you would do this.
更多回答(1 个)
Stephen23
2020-11-4
编辑:Stephen23
2020-11-4
The problem is not extracting the numbers (which is easy) but in knowing which of the numbers has been extracted, which is not a trivial task when different parts of the expression can be completely missing. But it can be done with regular expressions using optional grouping parentheses, which return empty strings if the content is not matched, allowing us to keep track of exactly which values have been matched:
% regular expression:
rgd = '\d+\.?\d*';
rge = '([eE][-+]?\d+)?';
rgx = ['^([-+]?NX)\*?(Te\^)?(?(2)[-+]?N)\*?(exp\()?',...
'(?(4)[-+]?N)(?(5)/\(?Te)?(?(6)[-+]N)?\)?\)?$'];
rgx = strrep(rgx,'N',rgd);
rgx = strrep(rgx,'X',rge);
% your input data:
str = {'6.8e-9*Te^0.67*exp(-4.4/(Te+0.5))','6.8e-9*exp(-4/Te)','1.2e7'};
tkn = regexp(str,rgx,'tokens','once');
tkn = vertcat(tkn{:})
format short g
mat = str2double(tkn(:,1:2:7));
mat(isnan(mat)) = 0
Note that this regular expression does not check for syntactic correctness, it can match other strings which are not syntactically correct expressions, i.e. it relies on your a priori knowledge about the input strings. And I had to make some guesses about the permitted syntaxes, which so far you have not formally defined.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!