regexp returns unwanted 0x0 string cells

24 次查看(过去 30 天)
Hello,
I have a string array c that contains :
c = ["v -0.618008 0.394531 -0.972656" "v -0.621094 0.391446 -0.972656" "n 0.064545 0.997915 -0.000000" "n 0.021679 0.999765 -0.000000"]
c = 1×4 string array
"v -0.618008 0.394531 -0.972656" "v -0.621094 0.391446 -0.972656" "n 0.064545 0.997915 -0.000000" "n 0.021679 0.999765 -0.000000"
From that, I want to extract all the v so that my result looks like that :
["v -0.618008 0.394531 -0.972656" "v -0.621094 0.391446 -0.972656"]
ans = 1×2 string array
"v -0.618008 0.394531 -0.972656" "v -0.621094 0.391446 -0.972656"
To do so I use this regex expression :
'v (-?[0-9. ]+){3}'
ans = 'v (-?[0-9. ]+){3}'
I tried it on regex101 and it gives me the right results. However when I use it with Matlab, it gives me two unwanted empty string cells like this :
regexp(c,'v (-?[0-9. ]+){3}','match')
ans = 1×4 cell array
{["v -0.618008 0.394531 -0.972656"]} {["v -0.621094 0.391446 -0.972656"]} {0×0 string} {0×0 string}
This is a problem because I want my end result to be an array of string so I want to use string() on this result, but string returns an error with the {0x0 string}...
Do you know how I can skip the empty string cells with the regexp function ?

采纳的回答

Walter Roberson
Walter Roberson 2022-7-6
编辑:Walter Roberson 2022-7-6
regexp() accepts for its first parameter:
  • character vector
  • string scalar
  • cell array of character vectors
  • nonscalar string array
In the case of nonscalar inputs, it returns a cell array of the output of processing each input.
Your 3rd and 4th string entries do not match anything, so processing them returns empty, leading to the empty cell entries.
This is how regexp is designed to operate.
You have two possibilities:
  • you can use logical indexing by ~cellfun(@isempty) to filter out the entries you do not want; or
  • you can leave the original lines unsplit, a character vector or string scalar, instead of string array. fileread() instead of readlines()

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by