1

For example , If My Input is : some text here 345 646 356 some text 235 5343 Output should be : some text here 345646356 some text 235 5343

In this case, it needs to remove spaces between numbers if length is 9. otherwise spaces should be as it is .

I have tried below command but it removes all the spaces between numbers if length is <9 or >9.

Input : my data is 345 245 254 and 454 356 34 and

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

Output : my data is 345245254 and 45435634 and

But I would need output as my data is 345245254 and 454 356 34 and

vijaya
  • 11
  • 2

2 Answers2

1

This is long but it works, assuming the string does not contain \x00 characters:

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;
0

Try with this REGEXP (edited):

  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"
                        )

(This does not solve the problem in all cases, see comments)

momobo
  • 1,755
  • 1
  • 14
  • 19
  • Hi Momodo. This is not working as expected. if my input value is 345 654 346 then output as per above logic is 345654346. But if my input is 345 654 346 64 then output as per above logic comes as 345654346 64. This is not as expected. It continuous numbers more than 9 it should nt consider that value as 9 digit. – vijaya Feb 25 '20 at 15:41
  • If I specify logic as below , pattern="(\\D\\s\\d)\\s*(\\d)\\s*(\\d)\\s*(\\d)\\s*(\\d)\\s*(\\d)\\s*(\\d)\\s*(\\d)\\s*(\\d\\s\\D)", Then it removed spaces only length of the continuous number is 9. But when record is just 9 digit with spaces. spaces are not removed from that number like input as 123 456 789 output as 123 456 789 since no alphabetic letter to compare – vijaya Feb 25 '20 at 16:02
  • Sorry, but my installation of ab initio refuse to have more than 9 capturing group in the regular expression. If the spaces could be anywhere, I think there is no solution in this way. If you know for example that the first three number (as in your example) are never separated by spaces, then we could spare capturing group and build a complete solutions. – momobo Feb 26 '20 at 17:50
  • Thanks Momobo for quick response. Spaces can be anywhere. the thing is i have to identify continuous 9 digit values as well as continuous 16 digit values excluding spaces in between them. – vijaya Feb 26 '20 at 19:21
  • And also this example removes spaces between digits final_value = re_replace( final_value , "((?<=\d) +(?=\d))" ,""); But if my input is like vijaya1 352 534 35 then it gives output as vijaya135253435. But my expectation here is vijaya1 35253435 Can u help me on resolving this issue – vijaya Feb 26 '20 at 19:23