MATLAB Answers

use regexp to extract index

10 views (last 30 days)
Nit C
Nit C on 10 Sep 2021
Commented: Nit C on 15 Sep 2021
trans_setup_2=ASSIGN {
# Blower speed in rpm
variable = SPEED
value = 1234
}
ASSIGN {
# Resulting time increment
variable = TIME_INCREMENT
value = 60 / SPEED / NR_BLADES / NR_TIME_STEPS_PER_BLADE
}
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 Comments
Nit C
Nit C on 15 Sep 2021
@Walter Roberson, Thanks. This solved my problem.

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 13 Sep 2021
hello
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 Comment
Nit C
Nit C on 13 Sep 2021
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.

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!