フィルターのクリア

Find char consecutive in a string

1 回表示 (過去 30 日間)
pamela sulis
pamela sulis 2015 年 11 月 10 日
コメント済み: pamela sulis 2015 年 11 月 11 日
Hi! I have this string
stringa={stringa1; stringa2; stringa3; stringa4};
stringa1='{a(abc)(ac)d(cf)}';
stringa2='{(ad)c(bc)(ae)}';
stringa3='{(ef)(ab)(df)cb}';
stringa4='{eg(af)cbc}';
and I want to find per every string (stringa1, stringa2 ..) the number of 'ab' 'ba'. a and b not must be consecutive. I try strfind but it's not give me the right answer. Can you help me? Thanks
  2 件のコメント
Guillaume
Guillaume 2015 年 11 月 10 日
I assume you mean a and b must not be consecutive?
Can you show what the answer should be for each of your example string? Can a 'a' be part of several 'ab' string or is it restricted to the closest 'b'.
If you ignore all the other characters is 'a***a***b***b' 1, 2 or 4 occurences?
pamela sulis
pamela sulis 2015 年 11 月 10 日
編集済み: pamela sulis 2015 年 11 月 10 日
yes, a e b must not b consecutive.
For example 'ab'
stringa1='{a(abc)(ac)d(cf)}'; --> I find 1 'ab'
stringa2='{(ad)c(bc)(ae)}'; --> I find 1 'ab'
stringa3='{(ef)(ab)(df)cb}'; --> I find 1 'ab'
stringa4='{eg(af)cbc}'; ---> I find 1 'ab'
For example 'ba'
stringa1='{a(abc)(ac)d(cf)}'; --> I find 1 'ba'
stringa2='{(ad)c(bc)(ae)}'; --> I find 1 'ba'
stringa3='{(ef)(ab)(df)cb}'; --> I find 0 'ba'
stringa4='{eg(af)cbc}'; ---> I find 0 'ba'
I want that the code makes the same... i try strfind but don't give me the answer that i want.
About your question: 'If you ignore all the other characters is 'a***a***b***b' 1, 2 or 4 occurences?' it is one occurences: i want know only if there are 'ab' or 'ba' not the number of time they are in the string.

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

採用された回答

Guillaume
Guillaume 2015 年 11 月 10 日
編集済み: Guillaume 2015 年 11 月 10 日
Thanks for clarifying. You still haven't my answered my question about whether or not a character can be part of several matches or if the matches can intersect (see my 'a***a***b***b' example).
Assuming the answer is no to both questions, the simplest way is to use a regular expression:
stringa1='{a(abc)(ac)d(cf)}';
stringa2='{(ad)c(bc)(ae)}';
stringa3='{(ef)(ab)(df)cb}';
stringa4='{eg(af)cbc}';
stringa={stringa1; stringa2; stringa3; stringa4};
matchstarts = regexp(stringa, 'a.+?b'); %for 'ab'
matchcounts = cellfun(@numel, matchstarts)
The regular expression above says match 'a', followed by as little as necessary (the ?) but at least one (the +) characters, followed by a 'b'.
For 'ba', the regular expression would then be 'b.+?a'
Note that if the answer is yes to either question, regular expressions won't work.
  5 件のコメント
Guillaume
Guillaume 2015 年 11 月 10 日
'(\a.+?b)\' is a meaningless regular expression.
I'm not clear on what you're trying to match anymore. Can you describe it clearly, in words, the same way I've explained my regular expression.
As I've said '\(a.+?b\)' will match an opening bracket, followed immediately by an 'a' followed by at least one character and as few as possible, followed by a 'b', immediately followed by a closing bracket. It will not match anything else.
pamela sulis
pamela sulis 2015 年 11 月 11 日
Thanks! I have solved!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by