How can I keep the first two elements from CSV values in a string

1 回表示 (過去 30 日間)
Gustavo Schwartz
Gustavo Schwartz 2024 年 10 月 1 日
コメント済み: Gustavo Schwartz 2024 年 10 月 2 日
I have a Table where one of the columns contains text elements separated by commas. I want to keep the first element (if there is only one) or the first two (if the are more than one). There also could be empty lines. For example, from:
abcd, efgh, tyui
[]
lkjh, poiu
wert
I want to get
abcd, efgh
[]
lkjh, pou
wert
I know how to do this with a for loop. But the idea would be to use regular expressions or something similar to accelerate the process. The Table has almost 2M elements.
Any suggestion would be of great help. Thanks.

採用された回答

Stephen23
Stephen23 2024 年 10 月 2 日
編集済み: Stephen23 2024 年 10 月 2 日
S = ["";"philosopher,historian,writer,political activist,literary critic";"philosopher";"philosopher,writer"]
S = 4x1 string array
"" "philosopher,historian,writer,political activist,literary critic" "philosopher" "philosopher,writer"
T = regexp(S,'^[^,]*(,[^,]+)?','match','once','emptymatch')
T = 4x1 string array
"" "philosopher,historian" "philosopher" "philosopher,writer"
  1 件のコメント
Gustavo Schwartz
Gustavo Schwartz 2024 年 10 月 2 日
Thanks Stephen23! This is exactly what I needed.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 10 月 1 日
FilteredData = regexp(YourTable.ColumnName, '^[^,]+(,\s+[^,]+)?', 'match', 'once');
  2 件のコメント
Gustavo Schwartz
Gustavo Schwartz 2024 年 10 月 2 日
Thanks, Walter, for your soon response. This regexp works to extract the words before the first comma, but I need to extract the words before the second comma (if there are more than one). Suppose I have:
"philosopher,historian,writer,political activist,literary critic"
"philosopher"
"philosopher,writer"
I want to get
"philosopher,historian"
"philosopher"
"philosopher,writer"
I would appreciate if you can provide a regexp to solve this problem.
Gustavo Schwartz
Gustavo Schwartz 2024 年 10 月 2 日
Aparently, this regexp works:
regexp(Table.Colum, '^[^,]*(?:,[^,]*)?','match')

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by