I'd like to use StringCount to count the number of subwords in a string that have the form **** where * is designed to stand for any character in a specified set:

starSet = {"A","B","C"};

For example:

string = "ABAACAWABCAA";

Would generate an output of 2 if Overlaps -> FALSE and a output of 3 is Overlaps -> TRUE. Is there a way to do this?

starSet = {"A", "B", "C"};
StringCount["ABAACAWABCAA", Repeated[starSet, {5}], 
            Overlaps -> False]


Alternatives is redundant, see the comment of Sjoerd C. de Vries below. Thanks ;)

| improve this answer | |
  • $\begingroup$ I was trying to find a duplicate but I've failed :/ $\endgroup$ – Kuba Apr 17 '14 at 11:01
  • 3
    $\begingroup$ I don't think Alternatives is necessary here. A list of patterns is already interpreted as alternatives. See under Details at the StringExpression page. $\endgroup$ – Sjoerd C. de Vries Apr 17 '14 at 11:11
  • $\begingroup$ @SjoerdC.deVries +1 :) I was not aware of that :) $\endgroup$ – Kuba Apr 17 '14 at 11:14
  • $\begingroup$ Not everyone is aware of Repeated. Just leave it so. $\endgroup$ – Sjoerd C. de Vries Apr 17 '14 at 14:49
  • $\begingroup$ @SjoerdC.deVries Ok, will stay then. $\endgroup$ – Kuba Apr 17 '14 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.