You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:在Zapier中通过正则遍历字典提取公司名称

Solving the Company Name Extraction via Regex in Zapier Python

Hey there! Let's break down how to traverse your email data dictionary and extract company names using regex in Zapier's Python code step.

First, Define Your Regex Pattern

First, you'll need a regex pattern tailored to the company names you're targeting. For example, if your company names follow formats like "Acme Corp", "Global Tech Ltd", or "Smith & Sons Inc.", a pattern like this might work:

import re

company_pattern = r'\b[A-Z][a-zA-Z&\'\s]+(?:Corp|Inc|Ltd|LLC|Co)\b'
# Adjust this pattern based on the actual company name formats you encounter!

Note: Test your regex with sample email content to make sure it catches all relevant cases—tweak the pattern to include any suffixes or formatting unique to your use case.

Traverse the Dictionary & Extract Matches

In Zapier, your input data (like email subject, body, sender info) is available in the input_data dictionary. You can either iterate through all key-value pairs in this dictionary, or target specific fields you know might contain company names (like body, subject, or from_name).

Here's a complete code example that handles both scenarios:

import re

def extract_companies(data):
    # Define your regex pattern here
    company_pattern = r'\b[A-Z][a-zA-Z&\'\s]+(?:Corp|Inc|Ltd|LLC|Co)\b'
    matches = set()  # Use a set to avoid duplicate company names
    
    # Option 1: Traverse ALL key-value pairs in the input dictionary
    for key, value in data.items():
        # Skip non-string values (like numbers or booleans)
        if isinstance(value, str):
            # Find all matches in the current field
            found = re.findall(company_pattern, value)
            if found:
                matches.update(found)
    
    # Option 2: Target specific fields only (uncomment if you prefer this)
    # target_fields = [data.get('body', ''), data.get('subject', ''), data.get('from_name', '')]
    # for field in target_fields:
    #     if isinstance(field, str):
    #         found = re.findall(company_pattern, field)
    #         matches.update(found)
    
    # Convert the set to a list (Zapier handles lists well for subsequent steps)
    return {"extracted_companies": list(matches) if matches else ["No company found"]}

# Run the function with Zapier's input data
output = extract_companies(input_data)

Key Details to Note:

  • Avoid Duplicates: Using a set() ensures you don't get repeated company names if the same name appears multiple times in the email (e.g., in both the subject and body).
  • Handle Edge Cases: The code skips non-string values and returns a fallback message if no matches are found—you can adjust this fallback to fit your workflow needs.
  • Zapier Compatibility: The function returns a dictionary with a key extracted_companies, which will be available as a data field in your next Zap steps (perfect for searching and updating your database).

Next Steps

Once you run this code, the extracted company names will be passed to your subsequent Zap actions. You can use this data to query your database and update the corresponding record as planned.

内容的提问来源于stack exchange,提问作者Ryan Miller

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:39:09