MATLAB Answers

use regexp to extract index

조회 수: 10(최근 30일)
Nit C
Nit C 2021년 9월 10일
댓글: Nit C 2021년 9월 15일
trans_setup_2=ASSIGN {
# Blower speed in rpm
variable = SPEED
value = 1234
# Resulting time increment
trans_setup_2 is a cell array created from the text file containing the above text.
I would like to extract an index of ASSIGN { # Blower speed in rpm (only first occurence ) and along with an index of string value = 60 / SPEED / NR_BLADES / NR_TIME_STEPS_PER_BLADE .
I have tried following:
ex='(?=ASSIGN).*|(?<=value = 60 / SPEED / NR_BLADES / NR_TIME_STEPS_PER_B).*'
ones=~cellfun(@isempty,regexp(trans_setup_2, ex, 'match'))
How should adapt 'ex' search pattern extactly to get only first occurence of the ASSIGN and then value = 60 / SPEED / NR_BLADES / NR_TIME_STEPS_PER_BLADE.
Please help me on this.
  댓글 수: 7
Nit C
Nit C 2021년 9월 15일
@Walter Roberson, Thanks. This solved my problem.

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

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 9월 13일
my 2 cents suggestion using readlines and working on strings :
this simple code can be expanded / modified according to what you need.
rr = readlines('trans_setup_2.txt');
rr_strip = strip(rr,'left'); % remove left blanks
a = find(strcmp(rr_strip,'ASSIGN {'));
b = find(strcmp(rr_strip,'value = 60 / SPEED / NR_BLADES / NR_TIME_STEPS_PER_BLADE'));
text_extract1 = rr(a(1):b);
  댓글 수: 1
Nit C
Nit C 2021년 9월 13일
I had strcmp used. But i am intersted to go with 'regexp' becuase there are many selection of text to make based on general pattern, keywords instead of extact text.

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

추가 답변(0개)




Community Treasure Hunt

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

Start Hunting!

Translated by