regexp match - not reading the hole name

1 回表示 (過去 30 日間)
Diana Acreala
Diana Acreala 2011 年 11 月 29 日
Hello!
I have a list o parameters and a txt file containing some parameters. I have to check if what is in the txt file is also in my list of parameters. For this I used: [a b c]=regexp(my_list_of_parameters, parameter_from_txt_file,'match')
In general it works, the variable a will contain the found parameter. The problem is that when in my txt file I have this parameter named: parameter1 and my list of parameters contain a parameter named: parameter1aedfwef; by using the code above, Matlab will say that parameter1 from my txt file exists in my list of parameters. When searching after parameter1, Matlab finds parameter1aedfwef and it stops after 1! It is not taking into consideration the hole name of the parameter. This is not good because is not the same parameter! What should I use for avoiding this?

採用された回答

Walter Roberson
Walter Roberson 2011 年 11 月 29 日
[a b c]=regexp(my_list_of_parameters, [parameter_from_txt_file '$'], 'match');
This will match the parameter from the text file only if it occurs at the end of the string. It will, though, match the parameter if something else occurs before it in the string, such as matching oldparameter1 . If you do not want that, if you want an exact match, put '^' at the beginning of the [] list I show.
Are you looking for exact matches between the parameter from the text file and the list of parameters? If so then you should consider using ismember() instead of regexp()
  1 件のコメント
Diana Acreala
Diana Acreala 2011 年 11 月 30 日
Thank you for your help! I used your code line and it's working well:)

サインインしてコメントする。

その他の回答 (1 件)

David Young
David Young 2011 年 11 月 29 日
Try using \<parameter1\> in the regular expression, instead of parameter1.
For example:
fileword = 'parameter1';
paramlist = 'parameter2, parameter1xyz, parameter3';
[a b c] = regexp(paramlist, ['\<' fileword '\>'], 'match')
This depends on parameter names being made of alphanumeric characters and underscore.

カテゴリ

Help Center および File ExchangeInstall Products についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by