Detecting a string and getting the entire sentence

I am a beginner to the 'regular expressions' in matlab. I am trying to detect a string in a document. Once the string is detected I want to get the entire sentence where string appears.
eg: Astr={'My name is X. I am working on Matlab. I work on it everyday. I do many mathematical analysis with matlab. Everyone should learn the language. It will be very useful. I think Matlab is fun.'}
So I want the output to be just those sentences that contain the string matlab: I am working on Matlab. I do many mathematical analysis with Matlab. I think Matlab is fun.
I know we can do this in a loop by getting the strings till we reach a '.' before and after the search string. But can I do it in a single statement by using regexp?
I tried
expr='\..+matlab.+\.'
[Amatch A1 A2]=regexpi(Astr,expr,'match');
It detects the first part \.. but not the end. Could you please tell me how to get the entire sentence - from one period to the next.
Thank you. Priya.

 Accepted Answer

Astr='My name is X. I am working on Matlab. I work on it everyday. I do many mathematical analysis with matlab. Everyone should learn the language. It will be very useful. I think Matlab is fun.';
Sentences=regexp(Astr,'\.','split');
Index=cellfun(@isempty,strfind(Sentences,'Matlab'));
FoundSentences=Sentences(~Index);

2 Comments

This only detects 2 sentences, but I think Priya wants to find both "matlab" and "Matlab".
Fangjun, thanks for the solution.. Yes I would be want to detect both matlab and Matlab but now I have an idea and can work on it.

Sign in to comment.

More Answers (2)

It is difficult to make a robust and general solution, because you have to define very carefully what makes the start and end of a sentence. What if a decimal number like 3.0 appears? The "." should not now signal the end of a sentence.
Setting aside such concerns, and dealing with the cases in your example, you could use this:
Astr='My name is X. I am working on Matlab. I work on it everyday. I do many mathematical analysis with matlab. Everyone should learn the language. It will be very useful. I think Matlab is fun.';
results = regexp(Astr, '[A-Z][^\.]*[Mm]atlab[^\.]*\.', 'match')
Note that like Fangjun Jiang I have defined the data as a simple string rather than as a string within a cell as in the question. You don't need a cell array unless you are handling multiple strings - as is the case for the results.
Also I have slightly altered what you requested by starting each sentence from an uppercase letter, rather than from the preceding ".". If you would like it to go from "." to ".", including a leading space, just omit the "A-Z" at the start of the expression.

1 Comment

Yes David I understand that just detecting '.' may lead me into trouble. I have just begun this project and still need to work out the kinks. Thanks for your solution, surely gives me start!

Sign in to comment.

s = ['My name is X. I am working on Matlab. I work on it everyday.'...
'I do many mathematical analysis with matlab.'...
'Everyone should learn the language. It will be very useful. '...
'I think Matlab is fun.'];
regexpi(s, '[\w\s,]*matlab[\w\s,]*[.;]+','match')

5 Comments

Wow, Oleg. That is impressive! I am kind of think that it is possible to do it with one line but I didn't even try. Seems regular expression helps you relax.
I am not really satisfied, can't get rid of the initial space.
The problem is, like David Yong said above, it is difficult to make a robust and general solution. It's hard to define a 'sentence'. What if the sentence contains the quote symbol and other valid symbols for phrases? I think the OP's interest is not at 'MATLAB'. Hope he's got some idea and be able to come up with a solution for his problem.
strtrim(regexpi()) would get rid of the leading whitespace.
To remove the leading whitespace without using strtrim after the fact.
regexpi(s, '\w[\w\s,]*matlab[\w\s,]*[.;]+','match')

Sign in to comment.

Categories

Find more on Programming 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!