regexp to extract the last underscore and the following letter (last letter) from words

조회 수: 5 (최근 30일)
I want to use regexp in order to extract only the last underscore and the last letter from the words below.
But if I have the words 'apple_c' and 'apple_man_man_h'
and I use regexp(words,'\_\w','split'),
then I get as a result 'apple' for both words, but it is wrong for me, because I want to extract onlythe last underscore and thr last letter, as I said.
The desired result for me is 'apple' and ''apple_man_man'.
How should i modify the command to achieve this?
Thank you

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 11월 15일
hello
I recognize I am not the regexp expert here , so I would suggest this alternative :
ind = findstr(words,'_');
word_out = words(1:ind(end)-1)
  댓글 수: 3
Stephen23
Stephen23 2021년 11월 15일
The behavior if there is no underscore might also be relevant:
words = 'apple';
ind = findstr(words,'_')
ind = []
word_out = words(1:ind(end)-1)
Array indices must be positive integers or logical values.
What is the desired output in this case?
Ioannis Vourvachakis
Ioannis Vourvachakis 2021년 11월 15일
Ηello Stephen, we talk about the case that all the words we study now contain an underscose and after the underscore follows the last letter of the word.

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by