# xarray.core.accessor_str.StringAccessor.count

#### StringAccessor.count(pat, flags=0, case=None)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/accessor_str.py#L1110-L1185)

Count occurrences of pattern in each string of the array.

This function is used to count the number of times a particular regex
pattern is repeated in each of the string elements of the
[`DataArray`](xarray.DataArray.html.md#xarray.DataArray).

The pattern pat can either be a single `str` or re.Pattern or
array-like of `str` or re.Pattern. If array-like, it is broadcast
against the array and applied elementwise.

* **Parameters:**
  * **pat** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str) or [`re.Pattern`](https://docs.python.org/3/library/re.html#re.Pattern) or [array-like](https://numpy.org/doc/stable/glossary.html#term-array_like) of [`str`](https://docs.python.org/3/builtins/stdtypes.html#str) or [`re.Pattern`](https://docs.python.org/3/library/re.html#re.Pattern)) – A string containing a regular expression or a compiled regular
    expression object. If array-like, it is broadcast.
  * **flags** ([`int`](https://docs.python.org/3/builtins/functions.html#int), *default*: `0`) – Flags to pass through to the re module, e.g. re.IGNORECASE.
    see [compilation-flags](https://docs.python.org/3/howto/regex.html#compilation-flags).
    `0` means no flags. Flags can be combined with the bitwise or operator `|`.
    Cannot be set if pat is a compiled regex.
  * **case** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default*: [`True`](https://docs.python.org/3/builtins/constants.html#True)) – If True, case sensitive.
    Cannot be set if pat is a compiled regex.
    Equivalent to setting the re.IGNORECASE flag.
* **Returns:**
  **counts** ([array](https://numpy.org/doc/stable/glossary.html#term-array) of [`int`](https://docs.python.org/3/builtins/functions.html#int))

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('cbb1fb6a-e779-4810-9000-58014bf42c8b','b267cb54-c0dc-44b0-8cb6-8f6d216b9393','f89d49f4-ea2c-4793-a453-ad2b4149860f','../lite/tree/../notebooks/index.html?path=2cbbaf2c_e120_40aa_818f_02a6a1aa215b.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> da = xr.DataArray(["jjklmn", "opjjqrs", "t-JJ99vwx"], dims="x")
>>> da
<xarray.DataArray (x: 3)> Size: 108B
array(['jjklmn', 'opjjqrs', 't-JJ99vwx'], dtype='<U9')
Dimensions without coordinates: x
```

Using a string:
>>> da.str.count(“jj”)
<xarray.DataArray (x: 3)> Size: 24B
array([1, 1, 0])
Dimensions without coordinates: x

Enable case-insensitive matching by setting case to false:
>>> counts = da.str.count(“jj”, case=False)
>>> counts
<xarray.DataArray (x: 3)> Size: 24B
array([1, 1, 1])
Dimensions without coordinates: x

Using regex:
>>> pat = “JJ[0-9]{2}[a-z]{3}”
>>> counts = da.str.count(pat)
>>> counts
<xarray.DataArray (x: 3)> Size: 24B
array([0, 0, 1])
Dimensions without coordinates: x

Using an array of strings (the pattern will be broadcast against the array):

```pycon
>>> pat = xr.DataArray(["jj", "JJ"], dims="y")
>>> counts = da.str.count(pat)
>>> counts
<xarray.DataArray (x: 3, y: 2)> Size: 48B
array([[1, 0],
       [1, 0],
       [0, 1]])
Dimensions without coordinates: x, y
```

<div id="f89d49f4-ea2c-4793-a453-ad2b4149860f" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('cbb1fb6a-e779-4810-9000-58014bf42c8b','f89d49f4-ea2c-4793-a453-ad2b4149860f')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('cbb1fb6a-e779-4810-9000-58014bf42c8b','f89d49f4-ea2c-4793-a453-ad2b4149860f')">Open In Tab</button></div><div id="b267cb54-c0dc-44b0-8cb6-8f6d216b9393" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
