The problem is the way the command you type is being parsed and executed. Each side of the pipe is executed inside a separate instance of cmd.exe
. Those instances do not execute the code you have typed as you have typed it, but a slightly different version that has been reinterpreted by the parser. In this step the aditional space has been included (here you can found a deeper analysis by dbenham)
We can not avoid the way the parser do its work, but we can generate a command where the added spaces do not affect the output
cmd /q /c "(for %f in ("Q:\Playlists\A 1*.m3u") do echo("%~nf")"| sed -E "s/.*/x&y/"