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

如何在OpenRefine中用正则匹配组批量生成对应新列?

Batch Generate Columns for Regex Capture Groups in OpenRefine

Great question—manually creating a column for each regex capture group is such a tedious chore, right? I’ve dealt with this exact scenario while cleaning datasets in OpenRefine, and there are two solid ways to automate this depending on whether you prefer using GREL (OpenRefine’s native language) or sticking with Jython.

Method 1: Use GREL (Fastest & Most User-Friendly)

OpenRefine has a built-in feature for adding multiple columns at once that’s perfect for this. Here’s how to use it:

  1. First, confirm your capture group count: Let’s say your regex is (\w+)-(\d+)-([A-Z]+)—that’s 3 capture groups. Make sure you’re only counting actual capture groups (non-capturing groups like (?:...) don’t count here).

  2. Select your target column, then go to Edit Columns > Add multiple columns based on this column.

  3. In the dialog box that pops up:

    • Column name pattern: Enter something like Group ${index}—this will auto-name your new columns Group 1, Group 2, etc.
    • Expression: Paste this GREL code, replacing /custom_regex/ with your actual regex (keep the slashes around it):
      if(isNull(value.match(/custom_regex/)), "No Match", value.match(/custom_regex/)[index - 1])
      
      The index variable is built into OpenRefine’s bulk column tool—it refers to the number of the column being created (starting at 1). Since GREL arrays are zero-indexed, we subtract 1 to grab the correct capture group.
    • Number of columns: Input the total number of capture groups you have (e.g., 3).
    • Hit OK, and OpenRefine will generate all your columns in one go!

Method 2: Use Jython Script (For Advanced Customization)

If you want to stick with Jython for more control (like handling edge cases with multiple matches), you can run a custom script to batch-create columns. Here’s how:

  1. Go to Project > Apply script in OpenRefine.
  2. Paste this script, replacing target_column and regex_pattern with your own values:
    import re
    
    # Configure your settings here
    target_column = "YourOriginalColumnName"
    regex_pattern = r"custom_regex"  # e.g., r"(\w+)-(\d+)-([A-Z]+)"
    regex = re.compile(regex_pattern)
    num_groups = regex.groups  # Auto-detects number of capture groups
    
    # Loop through each group and create a new column
    for group_idx in range(num_groups):
        new_col_name = f"Group {group_idx + 1}"
        # Define the Jython expression for the column
        column_expr = f"""
    import re
    regex = r"{regex_pattern}"
    matches = re.findall(regex, value)
    if matches:
        return matches[0][{group_idx}]
    else:
        return "No Match"
    """
        # Use OpenRefine's internal API to add the column
        grefine.column.add_column(
            project_id=project_id,
            column_name=new_col_name,
            base_column_name=target_column,
            expression=column_expr,
            language="jython"
        )
    
  3. Click Run—the script will automatically create a new column for each capture group.

Notes to Keep in Mind

  • If your regex can return multiple matches (not just one), adjust the matches[0] part in the code—for example, you could join all matches with a separator like ", ".join([match[group_idx] for match in matches]).
  • Double-check your regex to ensure you’re not including unintended capture groups (non-capturing groups (?:...) are safe here, as they don’t count towards regex.groups).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:12:13