> For the complete documentation index, see [llms.txt](https://trizen.gitbook.io/perl6-rosettacode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://trizen.gitbook.io/perl6-rosettacode/programming_tasks/c/count_occurrences_of_a_substring.md).

# Count occurrences of a substring

```perl
sub count-substring($big, $little) { +$big.comb: / :r $little / }
 
say count-substring("the three truths", "th"); # 3
say count-substring("ababababab", "abab");     # 2
 
say count-substring(123123123, 12);            # 3
```

The `:r` adverb makes the regex "ratchet forward" and skip any overlapping matches. `.comb` - when given a `Regex` as an argument - returns instances of that substring. Also, prefix `+` forces numeric context in Raku (it's a no-op in Perl 5). For the built in listy types that is the same as calling `.elems` method. One other style point: we now tend to prefer hyphenated names over camelCase.
