tl;dr
Skip the regex. Just use string manipulation, in a single statement.
Arrays
.stream( "aaa, bbb, ccc, ddd".split( ", " ) ) // Parse the text using COMMA with SPACE as the delimiter.
.map( ( String s ) -> s + " " + s + "_" ) // Change `aaa` to `aaa aaa_`.
.collect( Collectors.joining( ", " ) ); // Join the modified strings together as a single string.
aaa aaa_, bbb bbb_, ccc ccc_, ddd ddd_
String::split
No need for regex.
You can accomplish this goal by splitting the comma-separated words into an array of strings, make a stream of that array, and join them back together again with no extra terminator. And, you can do all than in a one-liner.
Given this input:
String input = "aaa, bbb, ccc, ddd";
…split the string into pieces.
String[] pieces = input.split( ", " ) ;
Stream
Make a stream of those pieces.
Stream < String > streamOfPieces = Arrays.stream( pieces ) ;
Take each element from the stream and transform each by adding a SPACE, the same string again, and the underscore you want at the end.
Stream < String > streamOfModifiedPieces = streamOfPieces.map( ( String s ) -> s + " " + s + "_" );
Collect results of stream
Terminate the stream by collecting all those transformed elements with a Collector. The Collector implementation we need is provided by a call to Collectors.joining. We pass our desired delimiter to that collector. In our case, the desired delimiter is a COMMA and a SPACE, ,. The collector is smart enough to include the delimiter between the elements yet omit from the end.
String output = streamOfModifiedPieces.collect( Collectors.joining( ", " ) );
output = aaa aaa_, bbb bbb_, ccc ccc_, ddd ddd_
One-liner
Combine all that into the promised one-liner.
String output = Arrays.stream( "aaa, bbb, ccc, ddd".split( ", " ) ).map( ( String s ) -> s + " " + s + "_" ).collect( Collectors.joining(", ") );
…or…
String output =
Arrays
.stream( "aaa, bbb, ccc, ddd".split( ", " ) )
.map( ( String s ) -> s + " " + s + "_" )
.collect( Collectors.joining( ", " ) );
aaa aaa_, bbb bbb_, ccc ccc_, ddd ddd_