To get all of the text inside multiple ''
blocks, regexp can be used as follows:
regexp(txt,'''(.[^'']*)''','tokens')
This says to get text surrounded by '
characters, which does not include a '
in the captured text. For example, consider this file with two lines (I made up different file name),
txt = ['.model sdata1 s tstonefile=''../data/s_element/isdimm_rcv_via_2port_via_minstub.s50p'' passive=2 ', char(10), ...
'.model sdata1 s tstonefile=''../data/s_element/isdimm_rcv_via_3port_via_minstub.s00p'' passive=2']
>> stringCell = regexp(txt,'''(.[^'']*)''','tokens');
>> stringCell{:}
ans =
'../data/s_element/isdimm_rcv_via_2port_via_minstub.s50p'
ans =
'../data/s_element/isdimm_rcv_via_3port_via_minstub.s00p'
>>
Trivia:
char(10)
gives a newline character because 10 is the ASCII code for newline.- The
.
character inregexp
(regex
in the rest of the coding word) pattern usually does not match a newline, which would make this a safer pattern. In MATLAB, a dot inregexp
does match a newline, so to disable this, we could add'dotexceptnewline'
as the last input argument to `regexp``. This is convenient to ensure we don't get the text outside of the quotes instead, but not needed since the first match sets precedent. - Instead of excluding a
'
from the match with[^'']
, the match can be made non-greedy with?
as follows,regexp(txt,'''(.*?)''','tokens')
.