find specific text in a text file

1 view (last 30 days)
amin on 13 Feb 2020
Commented: amin on 17 Feb 2020
In the text file below, I want to obtain everything between "|[" and "]|".
component1 |[ b,a,d, w,e,r ]| component2
so the output is : b,a,d, w,e,r
Thanks in advance.

Accepted Answer

Srivardhan Gadila
Srivardhan Gadila on 16 Feb 2020
Please refer to the functions fileread & regexp.
Let's assume that the following content
component1 |[ b,a,d, w,e,r ]| component2
is in a text file named textFile.txt then the following code might help you:
textChar = fileread("textFile.txt");
startIndex = regexp(textChar,'|[');
endIndex = regexp(textChar,']|');
requiredOutput = textChar(startIndex+1:endIndex-2)

More Answers (0)


Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!