1

例如,如果我的输入是: some text here 345 646 356 some text 235 5343 输出应该是: some text here 345646356 some text 235 5343

在这种情况下,如果长度为 9,则需要删除数字之间的空格。否则空格应保持原样。

我试过下面的命令,但如果长度<9或>9,它会删除数字之间的所有空格。

输入:我的数据是 345 245 254 和 454 356 34 和

逻辑:final_value = re_replace( final_value , "((?<=\d) +(?=\d))" ,"");

输出:我的数据是 345245254 和 45435634 和

但我需要输出,因为我的数据是 345245254 和 454 356 34 和

4

2 回答 2

1

这很长,但它可以工作,假设字符串不包含 \x00 字符:

out::clean_9_digits(instr)=
begin
  let string(int)[int] strv=string_split_no_empty(instr, " "); // length-prefixed to mark removed elements with "\0"
  let int i=0;
  while (i<length_of(strv))
    if ( not string_is_numeric(strv[i]))
      i = i + 1; // continue
    else
      begin
        let string(int) thisnum = strv[i];
        let int j = i + 1;
        while (j < length_of(strv) && string_is_numeric(strv[j]) )
          begin // concatenate all following numeric elements
            thisnum = string_concat(thisnum,strv[j]);
            j=j+1;
          end;
        if (length_of(thisnum) == 9) // match!
          begin
            strv[i]=thisnum; // replace first element with the 9 digit number
            j = i + 1; // mark remaining numeric elements that were combined
            while (j < length_of(strv) && string_is_numeric(strv[j]) ) 
              begin
                strv[j]="\0";
                j = j + 1;
              end;
          end;
        i=j+1; // continue at next element following numeric elements
      end;

  out :: string_replace(string_join(strv, " "), "\0 ", "");
end;

/*Reformat operation*/
out::reformat(in)=
begin
  out.instr :: in.instr;
  out.outstr :: clean_9_digits(in.instr);
end;
于 2020-03-08T23:50:29.297 回答
0

尝试使用此 REGEXP(已编辑):

  re_match_replace_all( str=in.final_value,
                        pattern="(\\D\\s*\\d)\\s*(\\d)\\s*(\\d)\\s*(\\d)\\s*(\\d)\\s*(\\d)\\s*(\\d)\\s*(\\d)\\s*(\\d\\s*\\D)",
                        replace_str="$1$2$3$4$5$6$7$8$9"
                        )

(这并不能在所有情况下都解决问题,请参阅评论)

于 2020-02-21T16:07:58.847 回答