countMatches

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

Counts the number of matches in a string.

Introduced in DataWeave version 2.4.0.

Parameters

Name Description

text

The string to search for matches.

pattern

A substring to find in the text.

Example

This example counts matches in a string.

Source

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

Output

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

Name Description

text

The string to search for matches.

pattern

The regex pattern to use in the search.

Example

This example counts the vowels in a string.

Source

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

Output

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.

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub