保留值前提下拆分含文字数字列的GREL实现求助
Got it, let's break this down clearly—this is a super common task, and once you know the right regex-based GREL tricks, it's straightforward. We'll split this into two safe steps (no data loss!) as you suggested:
1. Add a New Column for the Number + Trailing Content
First, we'll create a dedicated column to capture everything starting from the first digit in your original column. Here's how:
- In OpenRefine, select your target column → click Edit column → Add column based on this column
- Give the new column a name (like "Number & Rest")
- Paste this GREL expression into the formula box:
value.match(/\d.*/)[0]
What this does:
\dmatches the first digit in your text.*matches every character that comes after that digit (including more numbers, letters, symbols—whatever follows)match()returns an array of matches;[0]grabs the first (and only, in this case) match to populate the new column
Note: If there's sometimes a space before the first digit (e.g., "Hello World 456"), adjust the regex to /\s*\d.*/ to ignore leading whitespace before the digit.
2. Clean Up the Original Column to Keep Only Leading Text
Now that we've safely stored the numeric portion, let's strip that part out of the original column:
- Select your original column → click Edit cells → Transform
- Paste this GREL expression:
value.replace(/\d.*/, "")
What this does:
- This regex targets the same "first digit + everything after" pattern as before
replace()swaps that entire matched segment with an empty string, leaving only the text that came before the first digit
Example to Test With:
If your original cell has ProductABC789X, the new column will get 789X, and the original column will become ProductABC.
内容的提问来源于stack exchange,提问作者Will Hanley

