---
schema_version: "1"
id: 98bf705308d4237ba8f33618cd214a7709dee3f1aa3b96dfe5aa4e6cbc91bb96
title: countMatches
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.6/dw-strings-functions-countmatches.md
canonical_url: https://docs.mulesoft.com/dataweave/2.6/dw-strings-functions-countmatches
source_site: docs.mulesoft.com
content_type: documentation
content_hash: f640445a1f92018594d5da6ba2400293d38b51b3c26ee477b53826497c0fcfad
description: MuleSoft Documentation Site
release: "2.6"
latest: false
---
---
product: DataWeave
version: 2.6
---

# countMatches

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

## countMatches(text: String, pattern: String): Number

Counts the number of matches in a string.

_Introduced in DataWeave version 2.4.0._

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>text</code></p></td><td><p>The string to search for matches.</p></td></tr><tr><td><p><code>pattern</code></p></td><td><p>A substring to find in the text.</p></td></tr></tbody></table>

### Example

This example counts matches in a string.

#### Source

```DataWeave
%dw 2.0
import countMatches from dw::core::Strings
output application/json
---
"hello worlo!" countMatches "lo"
```

#### Output

```Json
2
```

## countMatches(text: String, pattern: Regex): Number

Counts the number of times a regular expression matches text in a string.

_Introduced in DataWeave version 2.4.0._

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>text</code></p></td><td><p>The string to search for matches.</p></td></tr><tr><td><p><code>pattern</code></p></td><td><p>The regex pattern to use in the search.</p></td></tr></tbody></table>

### Example

This example counts the vowels in a string.

#### Source

```DataWeave
%dw 2.0
import countMatches from dw::core::Strings
output application/json
---
"hello, ciao!" countMatches /[aeiou]/
```

#### Output

```Json
5
```

## countMatches(text: Null, pattern: Any): Null

Helper function that enables `countMatches` to work with a `null` value.

_Introduced in DataWeave version 2.4.0._
