Monday Mini-Golf #6: Meeesesessess upp teeexexextext Monday Mini-Golf: A series of short code-golf challenges, posted (hopefully!) every Monday.

True story1: The other day, I was playing around on my tablet when I had the idea to visit the page I normally use on my PC for testing JavaScript. After the page loaded, I entered this simple program:

alert("Hello!")


I then proceeded to press the Execute button, and was surprised when it told me that the code I had entered was invalid. I took a second look at the textbox and saw this:

alllelelerlerlertlert("Heeelelellellelloello!")


Wha??? That's not what I entered! So what happened here? To figure it out, I entered two simple lines:

abcdefg
0123456


This turned out as:

abbbcbcbcdbcdbcdebcdebcdefbcdefbcdefgbcdefg
0112123123412345123456


By now, I still had no clue about what happened to letters, but the numbers seemed simpler, so I took a closer look. As it turned out, the webpage was simply entering the first character, then repeating all the rest in the string every time a new one was pressed:

0112123123412345123456
0
 1
  12
    123
       1234
           12345
                123456


But what about the sections of letters? After pondering for a minute, I realized that it's just the same, but instead of repeating each subsection once, it repeats it twice:

abbbcbcbcdbcdbcdebcdebcdefbcdefbcdefgbcdefg
a
 bb
   bcbc
       bcdbcd
             bcdebcde
                     bcdefbcdef
                               bcdefgbcdefg


A combination of the two works with a combination of these techniques:

abc123z
abbbcbcbc1bc12bc123bc123zbc123z
a
 bb
   bcbc
       bc1
          bc12
              bc123
                   bc123zbc123z


Whatever glitch causes this seems to reset at punctuation and spaces, so abc def becomes abbbcbc deeefef.

By this point, I was so absorbed in figuring it out and turning it into an interesting challenge that I forgot why I had been on there in the first place. (I did figure out how to type normally, however: pressing space-backspace after every character. Pretty tedious, but you gotta do what you gotta do.)

Challenge

The goal of the challenge is to write a program or function that takes in the text to be processed, makes the changes listed above, and outputs/returns the result.

Details


The input will only contain printable ASCII, and no tabs or newlines.


Test-cases

Inputs: (one per line)

Mess up text
This is some longer text.
CAPS LOCK &amp;&amp; "Punc-tua"+'tion'
under_score_style
CaPs wItHIn loWERs
1337 numb3r5
abcdefghij 0123456789
Code-golf is the best!


Outputs:

Meeesesessess upp teeexexextext
Thhhihihishis iss sooomomomeome looononongongongeongeongeronger teeexexextext.
CAAAPAPAPSAPS LOOOCOCOCKOCK &amp;&amp; "Puuunununcunc-tuuuaua"+'tiiioioionion'
unnndndndendendernder_scccococorcorcorecore_stttytytyltyltyletyle
CaaaPaPaPsaPs wIIItItItHItHItHIItHIItHInItHIn loooWoWoWEoWEoWERoWERoWERsoWERs
1333337 nuuumumumbumbumb3umb3rumb3rumb3r5
abbbcbcbcdbcdbcdebcdebcdefbcdefbcdefgbcdefgbcdefghbcdefghbcdefghibcdefghibcdefghijbcdefghij 0112123123412345123456123456712345678123456789
Cooodododeode-gooolololfolf iss thhhehe beeesesestest!


Scoring

This is code-golf, so shortest valid code in bytes wins. Tiebreaker goes to submission that reached its final byte count first. The winner will be chosen next Monday, Nov 2. Good luck!

Edit: And the winner is... @MartinBüttner using Retina for an incredible 43-byte solution!

1 Yes, this story is completely true, and if you need any more clarification, see footnote 1.
Retina, 43 bytes

.(?&lt;=[^_\W]([^_\W]+))(?&lt;=(\1)(?&lt;=\D))?
$1$2


Run the code from a single file with the -s flag. Since this is just a single regex substitution, you can test it here (click the Context tab to see the results).

Explanation

This matches any digit and letter which is not the first in a run. While . can match any non-linefeed character, the lookbehinds ensure the other conditions:

(?&lt;=[^_\W]([^_\W]+))


This matches backwards from the position after the .. First it matches one or more alphanumeric characters and captures them into group 1 with ([^_\W]+). This immediately ensures that the . corresponds to an alphanumeric character itself. Then the additional [^_\W] makes sure that there is one more alphanumeric character in the current run, which we don't include the match. Now group 1 is what we want to replace the match with if it is a digit - if it is a letter we want to replace it with twice this string. That's where the second lookbehind comes into play:

(?&lt;=(\1)(?&lt;=\D))?


This one is optional, so if it fails it won't affect the match at all. It first ensures that the . was not a digit via (?&lt;=\D) - so this lookbehind is only relevant when we're matching letters. In that case, we match group \1 once more (this always matches, because we've captured the group from the same position), but capture it into group 2.

Hence, we simply replace the regex with the contents of both groups ($1$2). If the match was a digit, $2 will still be empty and we write back the prefix only once. If it was a letter, $2 is the same as $1 and we write it back twice.