I need only words when i split a string , while currently i am getting even spaces

string2=split(string(result{1,hh}(ss,1)),[" ",",","(",")","-"])
>> string2
string2 =
8×1 string array
"Secure"
"VPDM"
"to"
"Ebay"
"housing"
""
"optional"
""
I need the output to be
"Secure"
"VPDM"
"to"
"Ebay"
"housing"
"optional"

 採用された回答

Guillaume
Guillaume 2020 年 1 月 23 日
Probably the easiest is to do:
string2 = regexp(yourstring, '\w+', 'match')

10 件のコメント

Prajwal Venkatesh
Prajwal Venkatesh 2020 年 1 月 23 日
I can sometimes have numbers in there too
What should i do then?
Caution: \w will not match ' or - characters or period.
Don't pay $29.95 for e-mail!
Would get broken into
Don
t
pay
29
95
for
e
mail
It can be difficult to define what "words" are in English.
Prajwal Venkatesh
Prajwal Venkatesh 2020 年 1 月 23 日
also i want the output to be strings
Walter Roberson
Walter Roberson 2020 年 1 月 23 日
string() the cell array that regexp returns.
Walter Roberson
Walter Roberson 2020 年 1 月 23 日
\w already includes the digits. It would not include decimal points or commas or positive or negative signs or scientific notation. If you need to include numbers it is best to define exactly which numeric formats you will support.
Floating point numbers are always a nuisance to write expressions for, especially if you permit leading 0 to be omitted such as .716e-3 instead of 0.716e-3
Prajwal Venkatesh
Prajwal Venkatesh 2020 年 1 月 23 日
>> (regexp(string2, '\w+', 'match'))
ans =
8×1 cell array
{["Secure" ]}
{["VPDM" ]}
{["to" ]}
{["Ebay" ]}
{["housing" ]}
{0×0 string }
{["optional"]}
{0×0 string }
Error using string
Conversion from cell failed. Element 6 must be
convertible to a string scalar.
This is the issue
Can you attach your string2 and/or result in a .mat file so we can use it?
save('answers.mat', 'string2', 'result');
Walter Roberson
Walter Roberson 2020 年 1 月 23 日
Is string2 a string scalar or a non-scalar string array?
Prajwal Venkatesh
Prajwal Venkatesh 2020 年 1 月 24 日
I am attaching answers.mat
Walter Roberson
Walter Roberson 2020 年 1 月 24 日
string2 comes out as missing in that file, and there is no way that that result variable is the output of that regexp() command.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by