Search code examples
regexmatlabtextstrcmp

how to detect a part of expression in a string in matlab


I type as a user name v = ddd for example. I have then a string, name, of this shape:

aaa.bbb.ccc.ddd.eee 

and I want to detect if in my string name there exist (between dots) such string like from my input. How can I do it?

I tried the idea with

str = 'REGEXP.helps.you.relax';
user = 'el'
[m s e] = regexp(str, '\w*user\w*', 'match', 'start', 'end')

but I get error, because cannot put the variable 'user' this way inside regexp. How can I do it correctly? Or maybe is there any simpler idea?

Thanks!!


Solution

  • As an alternative to regexes, you can use strfind, which finds one string inside another. It returns the starting index of the child string (if it exists) and an empty matrix vector. Since you only need to check if that string exists or not, a simple solution is:

    ~isempty(strfind(str,user))
    % ans = 1