Teradata - 删除数字和某些标点符号,保留字母和其他标点符号

Teradata - Remove numbers and certain punctuation, leave alpha and other punctuation

我需要清理 Teradata 中的名称字段。有些条目不错:

其他还有数字,分号和pound/hash:

第二组示例是我需要清理的,但在姓氏和名字之间保留逗号,在中间首字母后保留句点。

我假设 REGEX_REPLACE 是我需要的,但找不到我正在尝试做的事情的例子。

在这里,我们可能希望从左边界 [A-Z] 开始,然后收集姓氏,后跟逗号和字符串的其余部分,表达式类似于:

(([A-Z].+)?,)\s*(.+)

Demo

正则表达式电路

jex.im 可视化正则表达式:

捕获组

const regex = /(([A-Z].+)?,)\s*(.+)/gm;
const str = `372;#Fishoder, Calvin Z.
5907;#Fishoder, Felix W.
43;#Francisco, Teddy A.
Belcher, Bob X.
Belcher, Linda A.
Pesto, Jimmy Z.`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

Regexp_replace 是你的朋友。您可以通过将多个字符放在方括号内来替换它们。所以如果你想替换 #; 或任何数字字符:

select
regexp_replace('AB,;#123','[;#0-9]','',1,0,'i')

在那个可爱的虚构示例中,您会回来的 AB,。我们删除了分号和数字。

运行 它与您的示例之一:

select
regexp_replace('372;#Fishoder, Calvin Z.','[;#0-9]','',1,0,'i')

回馈我们 Fishoder, Calvin Z.