I figure regex might be the best approach here. Since I don’t often deal with complex domains like .co.uk extensions, I think grabbing the final two parts (separated by a dot) should work fine for my needs.
Can anyone help me write a regex pattern that would work for this? I’m not great with regex so any guidance would be awesome.
For Zapier workflows, I use https?:\/\/(www\.)?([^.\/]+\.[^.\/]+) to grab the domain after stripping protocol and www. The second capture group is what you want. But if you need just the last two parts from any format, try ([^.\/\s]+\.[^.\/\s]+)(?:\/|$) instead - it pulls the final domain.extension combo from any URL structure. I’ve had problems when URLs have paths or query parameters, so the lookahead fixes that. Just make sure your Zapier regex step outputs the capture group, not the full match, or you’ll get the entire string back.
I’ve hit this same URL parsing issue in Zapier. Skip regex and try Zapier’s “Extract URL Domain” function in Text utilities first - it handles most cases automatically and you won’t want to pull your hair out. If you absolutely need regex, ([^.]+\.[^.]+)$ will grab those last two domain parts. But heads up - this breaks with complex TLDs like .co.uk or .com.au. You’ll get “co.uk” instead of “domain.co.uk”. Learned that one the hard way with international URLs. Test with a few real URLs from your data first - Zapier’s regex can be weird compared to other platforms.
regex is way overkill here. Just use javascript in zapier’s code step: new URL(inputUrl).hostname.split('.').slice(-2).join('.') - that’s it. much simpler than regex and handles edge cases better. works with all your url formats.