How can I get a non-determined specific part of a string?
1 view (last 30 days)
Show older comments
Dear all,
I have a big string containing a lot of different phrases. I need to get some phrases that I know how they begin and how they end.
e.g:
text='Hello. I'm a really big cat. { Attention: this string is a example.} Thats all';
How can I get the part between '{ Attention:' and '. }' ?
Like this:
Result='{ Attention: this string is a example.}';
I know how can I do it in a non-simple way, with loops and so, but I think that there is something with regexp that I don't know.
Thank you all
0 Comments
Accepted Answer
Image Analyst
on 16 Sep 2013
Maybe this?:
textString = 'Hello. I am a really big cat. { Attention: this string is a example.} Thats all';
leftBraceLocation = strfind(textString, '{')
rightBraceLocation = strfind(textString, '}')
textInBetween = textString(leftBraceLocation + 1: rightBraceLocation - 1)
0 Comments
More Answers (2)
Jan
on 16 Sep 2013
It depends. What do you want as result of:
'Hello. } { Attention: { Attention: example.{} { Attention: ';
If nested key as curly braces outside the wanted pattern are impossible, you could use:
ini = strfind(textString, '{ Attention:');
fin = strfind(textString, '}');
index = zeros(1, length(textString));
index(ini + 12) = 1;
index(fin) = -1;
result = textString(cumsum(index) ~= 0);
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!