How to access data of regexp output (without temporary variable)?
Mostra commenti meno recenti
I know how to convert the output from regexp( ) to a table by intermediating through a temporary variable as this solution:
strT = ["apple 001"; "banana 102"; "orange 344 001"];
C = regexp(strT, '\s', 'split', 'once'); % temporary variable
array2table(vertcat(C{:,:})) % desired output, except I hope there's a way to avoid braces.
I wonder if there is a way to cascade/pipeline function calls in a single line. It seems that similar questions had been asked zillion times (this post said so.) But I want to learn more about Matlab, so forgive me for asking it again.
% I tried this, but it doesn't give the desired result.
cell2table(regexp(strT, '\s', 'split', 'once'))
Risposta accettata
Più risposte (3)
albara
il 26 Apr 2023
Modificato: per isakson
il 26 Apr 2023
You can access the data of the regexp output directly without using a temporary variable by using the syntax [~,~,~,match] = regexp(str, expr), where str is the input string and expr is the regular expression you want to match.
The regexp function returns multiple outputs, including the start and end indices of the match, any tokens captured by the regular expression, and the matched substring. By using the ~ symbol as a placeholder for the outputs you don't need, you can ignore them and only keep the matched substring.
Here is an example:
% Define input string and regular expression
str = 'The quick brown fox jumps over the lazy dog';
expr = 'brown';
% Use regexp to find the matching substring and access it directly
[~, ~, ~, match] = regexp(str, expr);
% Display the matching substring
disp(match)
In this example, regexp is used to find the first occurrence of the word "brown" in the input string str. The ~ symbol is used as a placeholder for the outputs we don't need, and the match variable is used to store the matching substring directly.
The output of this code will be the string 'brown'. Note that you can access any of the other outputs of regexp by using the corresponding placeholder variables, such as [start, end, tokens, match] = regexp(str, expr) to store the start and end indices and any tokens in separate variables.
3 Commenti
albara
il 26 Apr 2023
Here's an other example:
less
strT = ["apple 001"; "banana 102"; "orange 344 001"];
array2table(cell2mat(cellfun(@(x) str2double(x), regexp(strT, '\s', 'split', 'once'), 'UniformOutput', false)))
This code will split the strings using regexp and then convert the resulting cell array of strings to a cell array of doubles using str2double. Then, cell2mat is used to convert the cell array to a matrix, and finally array2table is used to convert the matrix to a table. The output will be:
makefile
ans =
Var1 Var2
_____ ____
apple 1
banana 102
orange 344
Note that the second column contains only the first number in the original strings. If you want to keep the entire second column, you can modify the regular expression to capture the entire second field, like this:
less
array2table(regexp(strT, '(\S+)\s+(\S+)', 'tokens', 'once'))
This will give you the desired output:
makefile
ans =
Var1 Var2
________ _________
"apple" "001"
"banana" "102"
"orange" "344 001"
Stephen23
il 13 Mag 2023
"Why we have different results from the same codes?"
Within a regular expression the meta-character \S only matches non-whitespace characters:
"orange" "344 001"
% ^ so there is no way it could return this
If you insist on a single line, you can even use subsref to index the result of regexp. That will make your code hard to read and hard to modify.
Why don't you write a wrapper function you can call regexp2table like this:
strT = ["apple 001"; "banana 102"; "orange 344 001"];
regexp2table(strT, '\s', 'split', 'once') % Look, ma, one line
function tab=regexp2table(varargin)
% All inputs are piped to regexp().
C = regexp(varargin{:});
tab = array2table(vertcat(C{:}));
end
1 Commento
Simon
il 13 Mag 2023
Experimenting lead to this -
str = ["apple 001"; "banana 102 3579"; "orange 344 001"]
C = array2table([extractBefore(str,' ') extractAfter(str,' ')])
1 Commento
Simon
il 13 Mag 2023
Categorie
Scopri di più su Cell Arrays in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!