# xarray.DataArray.str.extract

#### DataArray.str.extract(pat, dim, case=None, flags=0)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/accessor_str.py#L1957-L2102)

Extract the first match of capture groups in the regex pat as a new
dimension in a DataArray.

For each string in the DataArray, extract groups from the first match
of regular expression pat.

If pat is 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.
  * **dim** ([hashable](../user-guide/terminology.html.md#term-name) or [`None`](https://docs.python.org/3/builtins/constants.html#None)) – Name of the new dimension to store the captured strings in.
    If None, the pattern must have only one capture group and the
    resulting DataArray will have the same size as the original.
  * **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.
  * **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.
* **Returns:**
  **extracted** (`same type as values` or `object array`)
* **Raises:**
  * [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError) – pat has no capture groups.
  * [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError) – dim is None and there is more than one capture group.
  * [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError) – case is set when pat is a compiled regular expression.
  * [**KeyError**](https://docs.python.org/3/builtins/exceptions.html#KeyError) – The given dimension is already present in the DataArray.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('5cfe79c8-c1e5-47e2-9c56-22991090612b','41affa64-deff-4941-b01c-0aab0cb412d2','5f282c26-c637-4e81-95c3-04afa05a238b','../lite/tree/../notebooks/index.html?path=83124f3a_b17f_40fe_99ce_0f1a9f0cabc8.ipynb','None')">Try it in your browser!</button></div>

Create a string array

```pycon
>>> value = xr.DataArray(
...     [
...         [
...             "a_Xy_0",
...             "ab_xY_10-bab_Xy_110-baab_Xy_1100",
...             "abc_Xy_01-cbc_Xy_2210",
...         ],
...         [
...             "abcd_Xy_-dcd_Xy_33210-dccd_Xy_332210",
...             "",
...             "abcdef_Xy_101-fef_Xy_5543210",
...         ],
...     ],
...     dims=["X", "Y"],
... )
```

Extract matches

```pycon
>>> value.str.extract(r"(\w+)_Xy_(\d*)", dim="match")
<xarray.DataArray (X: 2, Y: 3, match: 2)> Size: 288B
array([[['a', '0'],
        ['bab', '110'],
        ['abc', '01']],

       [['abcd', ''],
        ['', ''],
        ['abcdef', '101']]], dtype='<U6')
Dimensions without coordinates: X, Y, match
```

<div id="5f282c26-c637-4e81-95c3-04afa05a238b" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('5cfe79c8-c1e5-47e2-9c56-22991090612b','5f282c26-c637-4e81-95c3-04afa05a238b')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('5cfe79c8-c1e5-47e2-9c56-22991090612b','5f282c26-c637-4e81-95c3-04afa05a238b')">Open In Tab</button></div><div id="41affa64-deff-4941-b01c-0aab0cb412d2" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>

#### SEE ALSO
[`DataArray.str.extractall`](xarray.DataArray.str.extractall.html.md#xarray.DataArray.str.extractall), [`DataArray.str.findall`](xarray.DataArray.str.findall.html.md#xarray.DataArray.str.findall), [`re.compile`](https://docs.python.org/3/library/re.html#re.compile), [`re.search`](https://docs.python.org/3/library/re.html#re.search), [`pandas.Series.str.extract`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.extract.html#pandas.Series.str.extract)
