Skip to main content
3 of 4
added 1509 characters in body
NinjaBearMonkey
  • 10.4k
  • 3
  • 38
  • 67

Retina, 50 41 bytes

M&!r`.+ O`\G..+¶ s`(.*)¶.¶(.*) $2¶$1 ¶.$ 

Trailing newline significant. Takes input with the two strings separated by a newline:

Test Testing 

Try it online!

Explanation

M&!r`.+ 

The first line generates the "steps" of both words:

Testing Testin Testi Test Tes Te T Test Tes Te T 

M is for match mode, & considers overlapping matches, and ! prints the matches instead of the number of them. The reason it's reversed is the right-to-left option: the engine starts looking for matches at the end of the string and continues toward the beginning.

O`\G..+¶ 

This reverses the lines of the first half (second input word). It actually sOrts the lines, and the regex limits the matches: it must be a line of two or more characters (..+) followed by a newline () that begins where the last one left off (\G). In the above example, the single T in the middle doesn't match, so nothing after it can.

Te Tes Test Testi Testin Testing T Test Tes Te T 

Now we have the right two components, but in the wrong order.

s`(.*)¶.¶(.*) $2¶$1 

¶.¶ matches the lone T in the middle, which we don't need but separates the two parts. The two (.*) capture everything before and after, including newlines thanks to single-line mode. The two captures are substituted in the right order with a newline in between.

Now we're done, unless the input strings are one character long, in which case the input hasn't changed. To get rid of the duplicate, we replace ¶.$ (when the last line of the string a single character) with nothing.

NinjaBearMonkey
  • 10.4k
  • 3
  • 38
  • 67