如何在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:
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).Select your target column, then go to Edit Columns > Add multiple columns based on this column.
In the dialog box that pops up:
- Column name pattern: Enter something like
Group ${index}—this will auto-name your new columnsGroup 1,Group 2, etc. - Expression: Paste this GREL code, replacing
/custom_regex/with your actual regex (keep the slashes around it):
Theif(isNull(value.match(/custom_regex/)), "No Match", value.match(/custom_regex/)[index - 1])indexvariable 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!
- Column name pattern: Enter something like
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:
- Go to Project > Apply script in OpenRefine.
- Paste this script, replacing
target_columnandregex_patternwith 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" ) - 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 towardsregex.groups).
内容的提问来源于stack exchange,提问作者MehmedB

