0

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!!

3
  • 1
    is el really an acceptable match, or shouldn't it be help? Commented Sep 25, 2012 at 16:25
  • @Jonas Why so? Why not relax? Commented Sep 25, 2012 at 16:28
  • @yoda: because I stop parsing once I found a match. Commented Sep 25, 2012 at 16:29

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

4 Comments

what if I want to compare with one cell from a 2x2 cell, for example ~isempty(strfind(str,user(2,2))), doesn't work ... i get such an error: If any of the input arguments are cell arrays, the first must be a cell array of strings and the second must be a character array.
@user1578163 I don't follow... could you update your question with an example illustrating your problem?
question is the same. but try to put into workspace this: user{1,1} = 'al', user{1,2} = 'el', user{2,1} = 'ul', user{2,2} = 'ol'. if I want then to compare user{1,2} with the string, your answer doesn't work, i get an error. how should i write it then?
@user1578163 cellfun(@(x)~isempty(strfind(str,x)),user)
3

Use ['\w*' user '\w*'] as your second argument, and it should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.