Extract Variables from mixed string

조회 수: 2 (최근 30일)
Azar Nejad
Azar Nejad 2018년 8월 12일
댓글: Azar Nejad 2018년 8월 12일
Hi all,
In this string
'# Message: onset_pic1_8.png'
how can I read 'onset', 'pic', '1', '8' in four variables?

채택된 답변

Walter Roberson
Walter Roberson 2018년 8월 12일
S = '# Message: onset_pic1_8.png';
parts = regexp(S, '(?<name1>[A-Za-z]+)_(?<name2>[A-Za-z]+)(?<num1>\d+)_(?<num2>\d+)', 'names')
parts =
struct with fields:
name1: 'onset'
name2: 'pic'
num1: '1'
num2: '8'
I coded to permit uppercase as well as lowercase, but I did assume that the alphabetic parts remain alphabetic and the numeric parts remain numeric.
  댓글 수: 1
Azar Nejad
Azar Nejad 2018년 8월 12일
This works great many thanks
Azar

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

추가 답변 (1개)

Paolo
Paolo 2018년 8월 12일
편집: Paolo 2018년 8월 12일
You can use:
mystring = '# Message: onset_pic1_8.png';
matches = regexp(mystring,'\d|[a-z]+(?=_|\d)','match')
>>matches
{'onset'} {'pic'} {'1'} {'8'}
To obtain the values.
You can't use 1 and 8 as variable names.

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by