【问题标题】:Teradata - Remove numbers and certain punctuation, leave alpha and other punctuationTeradata - 删除数字和某些标点符号,留下字母和其他标点符号
【发布时间】:2019-10-29 12:10:15
【问题描述】:

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

  • 贝尔彻,鲍勃 X。
  • 贝尔彻,琳达 A.
  • 香蒜酱,吉米 Z.

其他也有数字、分号和英镑/哈希:

  • 372;#Fishoder, Calvin Z.
  • 5907;#Fishoder, Felix W.
  • 43;#Francisco, Teddy A.

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

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

【问题讨论】:

  • 你可以使用这个简单的正则表达式:[A-Z].* 并设置多行选项,如果有多行的话

标签: regex teradata


【解决方案1】:

在这里,我们可能希望从左边界[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}`);
    });
}

【讨论】:

    【解决方案2】:

    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.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-31
      • 2018-01-14
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      相关资源
      最近更新 更多