regexp split at first whitespace

조회 수: 41 (최근 30일)
Vincent Scalfani
Vincent Scalfani 2018년 7월 14일
댓글: Simon 2023년 4월 25일
I have a cell dataset like this, where it is a string of letters and numbers all in one column (many rows):
data =
'0R 2 2 0'
'1R 2 0 0 4'
'2R 2 2 0 1 1 1'
'3R 2 2 2 1 1'
I would like to split each row into 2 columns at the first space:
data =
'0R' '2 2 0'
'1R' '2 0 0 4'
'2R' '2 2 0 1 1 1'
'3R' '2 2 2 1 1'
I tried:
splitcells = regexp(data, '\s+','split');
but that splits at each space, creating numerous columns, not 2. How do I get the regular expression to split only at the first space? Thanks

채택된 답변

Paolo
Paolo 2018년 7월 14일
You may use the once option to split only once at the first occurrence of whitespace.
splitcells = regexp(data,'\s','split','once')
  댓글 수: 2
Vincent Scalfani
Vincent Scalfani 2018년 7월 14일
Thanks so much! If anyone else has this question, you can use the following to unnest the 1x2 cells from the split:
data = vertcat(data{:});
Simon
Simon 2023년 4월 25일
I have the same problem. Thanks for the answers!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by