Hey everyone! I’m stuck with a Python problem in my Zapier workflow. I’m trying to get all the 9-digit IDs from a URL, but my code only grabs the first one. Here’s what I’ve got so far:
import re
urls = re.findall(r'\b\d+\b', input_data['input'])
return [
{'url': url} for url in urls
]
The input looks like this: https://api.helpscout.net/v1/conversations/123456789,098765432.json
I want to get both IDs (123456789 and 098765432) so I can rebuild URLs for each one later. Is my regex off? Or am I returning the results wrong? I’m pretty new to Python, so any help would be awesome. Thanks!
hey sky24, i think ur regex is close but not quite right. try changing it to r’\b\d{9}\b’. this’ll grab all 9-digit numbers in the url. also, make sure ur using re.findall() to get all matches, not just the first one. hope that helps!
I’ve dealt with similar regex issues before, and I think I can help. Your approach is on the right track, but you need to tweak the regex pattern a bit. Instead of r’\b\d+\b’, try using r’\b\d{9}\b’. This will specifically match 9-digit numbers.
Here’s how I’d modify your code:
import re
urls = re.findall(r'\b\d{9}\b', input_data['input'])
return [{'url': url} for url in urls]
This should grab all the 9-digit IDs from your URL. I’ve used this pattern in several projects, and it’s always worked well for me. Just remember to test it thoroughly with different inputs to make sure it catches all the cases you need. Let me know if you run into any other issues!
Your regex pattern is close, but it isn’t specific enough for 9-digit IDs. Modify it by using r'\b\d{9}\b', which will ensure that exactly 9 digits are matched and are bounded by word boundaries. This adjustment guarantees that all non-overlapping sequences of 9 digits are captured when using re.findall().
For example:
import re
urls = re.findall(r'\b\d{9}\b', input_data['input'])
return [{'url': url} for url in urls]
Testing with various inputs is recommended to ensure the pattern behaves as expected.