2

I am trying to get a space into every 4th number/digit (not character). This is what I come up with:

  newStudentNumber := regexp_replace(newStudentNumber, '[[:digit:]](....)', '\1 ');
  dbms_output.put_line(newStudentNumber);

result:

NL 2345 7894  TUE

What I actually want:

NL 1234 5678 944 TUE

My code replaces the number at every 4th place with a spacebar, instead of adding a space like the wanted result above.

Can anyone explain this to me?

Thanks in advance

Maxime de Lange
  • 287
  • 2
  • 7
  • 21

1 Answers1

3

You can use the following regex..

([[:digit:]]{4})

And replace with what you are doing now.. \1(space)

Why yours is not working?

Your regex matches a digit and captures next 4 characters (not only digits). So.. when you do a replace.. the digit which is matched but not captured is also replaced.. and not because it is unable to insert.

Explanation for input = NL 12345678944 TUE and regex = [[:digit:]](....):

NL 12345678944 TUE   (it will match digit "1" and captures "2345")

See DEMO

karthik manchala
  • 13,492
  • 1
  • 31
  • 55
  • When I tried your solution, I get this: NL 944 TUE The value of newStudentNumber = NL 12345678944 TUE and I want to change that to NL 1234 5678 944 TUE – Maxime de Lange May 17 '15 at 11:47
  • Did you check the demo? did you use `newStudentNumber := regexp_replace(newStudentNumber, '([[:digit:]]{4})', '\1 ');`? – karthik manchala May 17 '15 at 11:51
  • I checked out the demo ye. I honestly did not understand that, but thanks for providing that demo anyways. I also would like to thanks you for the solution. It works. Thanks alot and I mark your awnser as the right given one. cheers – Maxime de Lange May 17 '15 at 11:56