Extract only text between quotes of a string

64 views (last 30 days)
Folks, could use an assist. New to REGEXP but persistent. Desire only the literal text between quotes for this string:
imt = "e";
[subchunk] = regexp(textline,'\".*?\"','match','ignorecase');
imt = subchunk;
When I return the argument from my readtext file function, and print using disp(imt), I get this:
'"e"'
When I only want:
e
I assume the single quotes are associated with the disp() function?

Accepted Answer

Walter Roberson
Walter Roberson on 1 Mar 2012
Edited: John Kelly on 26 Feb 2015
subchunk = regexp(textline, '(?<=")[^"]+(?=")', 'match');
imt = subchunk{1};
You could also consider just using a basic strfind() for '"', removing everything up to the first match and everything from the second match on.
  8 Comments

Sign in to comment.

More Answers (1)

Pierre Harouimi
Pierre Harouimi on 8 Mar 2022
From R2020b, you can use pattern, much easier than the complicated regexp function.
For your pb, you the extractBetween function seems the best one:
subchunk = extractBetween("'e'", "'", "'")

Community Treasure Hunt

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

Start Hunting!

Translated by