Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

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


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

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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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