Tallying specific patterns in a text string using Zapier automation

Hey everyone! I’m trying to figure out how to count certain word patterns in a string using Zapier. I’ve cleaned up the string with the formatter, but I’m stuck on the counting part.

Here’s what I’m working with:

[{FirstName:Alice,LastName:Johnson},{FirstName:Bob,LastName:Williams},{FirstName:Carol,LastName:Davis},{FirstName:Dan,LastName:Brown}]

I want to count how many times ‘FirstName’ appears (which should be 4 in this case).

I’ve looked at word-counting examples, but I can’t get them to work in Zapier’s code action. I’m not a coder, so I’m hoping someone can help me with a simple JavaScript or Python script to use in Zapier.

Any tips or a basic script would be super helpful! Thanks in advance!

I’ve tackled similar challenges in Zapier before, and I can offer an alternative approach using Python in the Code action. Here’s a script that should work for your scenario:

input_string = input_data['your_input_variable']
count = input_string.count('FirstName')
output = {'count': count}

This script is straightforward and doesn’t rely on regular expressions, which can be tricky for newcomers. It uses the built-in ‘count’ method of strings, which is efficient and easy to understand.

Remember to replace ‘your_input_variable’ with the actual variable name holding your input string in Zapier. This method is particularly useful when you need to count non-overlapping occurrences of a substring.

Always test your script with various inputs to ensure it behaves as expected in different scenarios.

As someone who’s worked extensively with Zapier automations, I can share a simple solution for your pattern counting problem. In Zapier’s Code action, you can use JavaScript to achieve this. Here’s a straightforward script that should do the trick:

const inputString = inputData.yourStringVariable;
const count = (inputString.match(/FirstName/g) || []).length;
return {count: count};

Replace ‘yourStringVariable’ with the actual variable name containing your input string. This script uses a regular expression to find all occurrences of ‘FirstName’ and counts them.

I’ve found this method to be reliable across various Zapier workflows. It’s simple enough for non-coders to implement and modify as needed. Just make sure to test it thoroughly with different input strings to ensure it works consistently in your specific use case.

hey emma! i’ve done this b4. try this js snippet in zapier:

const str = inputData.yourString;
const count = str.split(‘FirstName’).length - 1;
return {count};

it’s super simple n works great. just swap ‘yourString’ with ur actual input variable. hope this helps!