4
$\begingroup$

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"};
modifiedStringCount[string,"*****"]

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?

$\endgroup$

1 Answer 1

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

Edit:

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

$\endgroup$
5
  • $\begingroup$ I was trying to find a duplicate but I've failed :/ $\endgroup$
    – Kuba
    Commented Apr 17, 2014 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$ Commented Apr 17, 2014 at 11:11
  • $\begingroup$ @SjoerdC.deVries +1 :) I was not aware of that :) $\endgroup$
    – Kuba
    Commented Apr 17, 2014 at 11:14
  • $\begingroup$ Not everyone is aware of Repeated. Just leave it so. $\endgroup$ Commented Apr 17, 2014 at 14:49
  • $\begingroup$ @SjoerdC.deVries Ok, will stay then. $\endgroup$
    – Kuba
    Commented Apr 17, 2014 at 14:56

Your Answer

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

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