Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting extra line using row seperator

I'm using this regex to split one line into multiple group by 2 characters.

string.replaceAll("(.{2})", "$1\r\n")

Here is an example.

Like my input input string is like this

ABCDEFGHIJ

And output am getting like this

Output
->AB
->CD
->EF
->GH
->IJ
->

Is there any way so that I don't receive that last empty line?

like image 998
kelly Avatar asked Jul 05 '26 18:07

kelly


1 Answers

You have two options:

  • Manually remove the new line from the result
  • Change your regex to: (.{2})(?!$)

Now the regex will catch all pairs except the last one, and you will not be replacing it with a new line.

like image 111
Maroun Avatar answered Jul 07 '26 08:07

Maroun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!