[Question]
  [
**Monday Mini-Golf:** A series of short [code-golf](/questions/tagged/code-golf "show questions tagged '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](http://www.webtoolkitonline.com/javascript-tester.html) 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 && "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 && "Puuunununcunc-tuuuaua"+'tiiioioionion'
unnndndndendendernder_scccococorcorcorecore_stttytytyltyltyletyle
CaaaPaPaPsaPs wIIItItItHItHItHIItHIItHInItHIn loooWoWoWEoWEoWERoWERoWERsoWERs
1333337 nuuumumumbumbumb3umb3rumb3rumb3r5
abbbcbcbcdbcdbcdebcdebcdefbcdefbcdefgbcdefgbcdefghbcdefghbcdefghibcdefghibcdefghijbcdefghij 0112123123412345123456123456712345678123456789
Cooodododeode-gooolololfolf iss thhhehe beeesesestest!
```
# Scoring
This is [code-golf](/questions/tagged/code-golf "show questions tagged '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.
  
[Answer]
# [Retina](https://github.com/mbuettner/retina), 43 bytes
```
.(?<=[^_\W]([^_\W]+))(?<=(\1)(?<=\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](http://regexstorm.net/tester?p=.(%3f%3c%3d%5b%5e_%5cW%5d(%5b%5e_%5cW%5d%2b))(%3f%3c%3d(%5c1)(%3f%3c%3d%5cD))%3f&i=Mess+up+text%0d%0aThis+is+some+longer+text.%0d%0aCAPS+LOCK+%26%26+%22Punc-tua%22%2b%27tion%27%0d%0aCaPs+wItHIn+loWERs%0d%0a1337+numb3r5%0d%0aabcdefghij+0123456789%0d%0aCode-golf+is+the+best!&r=%241%242) (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:
```
(?<=[^_\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:
```
(?<=(\1)(?<=\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 `(?<=\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.
[Answer]
# JavaScript(ES6) 82
Using the regexp optimization suggested by Mwr247
```
s=>s.replace(/([\W_])|./g,(x,y)=>y?(f=0,x):f?(p+=x,p+(-x-1?z:p)):(p=z,f=x),f=z='')
```
Test running the snippet below in any recent browser
```
F=s=>s.replace(/([\W_])|./g,(x,y)=>y?(f=0,x):f?(p+=x,p+(-x-1?z:p)):(p=z,f=x),f=z='')
// document.write(F(prompt('Insert string')))
// TEST
console.log=x=>O.innerHTML+=x+'\n'
function test()
{
  O.innerHTML=F(I.value)
}
;[
 ['Mess up text','Meeesesessess upp teeexexextext']
,['This is some longer text.',
  'Thhhihihishis iss sooomomomeome looononongongongeongeongeronger teeexexextext.']
,['CAPS LOCK && "Punc-tua"+\'tion\'',
  'CAAAPAPAPSAPS LOOOCOCOCKOCK && "Puuunununcunc-tuuuaua"+\'tiiioioionion\'']
,['CaPs wItHIn loWERs'
  ,'CaaaPaPaPsaPs wIIItItItHItHItHIItHIItHInItHIn loooWoWoWEoWEoWERoWERoWERsoWERs']
,['1337 numb3r5','1333337 nuuumumumbumbumb3umb3rumb3rumb3r5']
,['abcdefghij 0123456789'
 ,'abbbcbcbcdbcdbcdebcdebcdefbcdefbcdefgbcdefgbcdefghbcdefghbcdefghibcdefghibcdefghijbcdefghij 0112123123412345123456123456712345678123456789']
,['Code-golf is the best!'
  ,'Cooodododeode-gooolololfolf iss thhhehe beeesesestest!']
].forEach(t => (
  i=t[0],x=t[1],r=F(i),
  console.log('Test ' + (r==x?'OK':'Fail')+'\nInput:  '+i+'\nResult: '+r+'\nCheck:  '+x+'\n')
))
```
```
#I { width: 50% }
```
```
Your test<input id=I><button onclick="test()">-></button>
<pre id=O></pre>
```
[Answer]
# JavaScript (ES6), ~~92~~ ~~88~~ 87
```
f=>f.replace(/[^_\W]+/g,m=>m[0]+[...m].slice(1).reduce((a,e)=>a+=(y+=e,++e?y:y+y),y=''))
```
I have no idea how to golf this down...
*Thanks Mwir247 for the golfed down regex and ETHproductions for one byte golf in reduce function*.
[Answer]
# Gema, 57 characters
```
\X?=?@set{s;}
<D1>=@append{s;$0}$s
<L1>=@append{s;$0}$s$s
```
Sample run:
```
bash-4.3$ gema '\X?=?@set{s;};<D1>=@append{s;$0}$s;<L1>=@append{s;$0}$s$s' <<< '1337 numb3r5'
1333337 nuuumumumbumbumb3umb3rumb3rumb3r5
```
[Answer]
# Haskell, ~~215~~ 200 bytes
```
import Data.List
import Data.Char
c=concatMap
p c=[isDigit c,isAlpha c]
r(Just n)=c id.replicate(n+1)
r _=id
f(h:t)=h:(c(\s->r(findIndex id.p$last s)s).tail.inits)t
m=c f.groupBy(\a b->or$p a==or$p b)
```
It's a mess.
Have fun reading all the weird quirks, I never thought I would use `id` this much.
Too bad the stuff that already does half the work here needs to be imported (`isDigit`,`isAlpha`,`inits`).
[Answer]
# Haskell, ~~183~~ 181 bytes
```
import Data.Char
b(x:y)|h x=x:c[]y|2>1=x:b y
b[]=[]
h y=isDigit y||isAlpha y
c x l@(y:z)|h y=c(y:x)z|2>1=c x[]++b l
c x[]=reverse$g$x
g[]=[]
g a@(x:y)|isDigit x=a++g y|2>1=a++a++g y
```
Usage:
```
b"Hello, world!"
>"Heeelelellellelloello, wooorororlorlorldorld!"
```
Don't know if it is essentially different with Leif Willerts' answer. But here's the algorithm.
1. Search for valid letter(Letter or Num)
2. If it letter, begin collecting letter. However, it is stored reversely, make the string reversed. And first item is omitted.
3. As it hit invalid letter, do the question's transform with reversed input. The question's transform is reverse.g.reverse, but since it is already reversed, then we just do reverse.g
I somehow keep redudant brackets.
[Answer]
**T-SQL, 357 bytes**
```
create proc x(@s varchar(99),@z int=0,@i int=0,@y varchar(99)='',@x varchar(99)='',@w varchar(99)='') as
while LEN(@s)>0
begin
    select @z=patindex('%[^0-z]%',@s+' '),@i=1,@w+=LEFT(@s,1)
    while @i<@z-1
    begin
        select @y=SUBSTRING(@s,2,@i)
        ,@x+=@y            
        ,@i+=1
        if @y like'%[a-z]'set @x+=@y
    end
select @w+=@x+SUBSTRING(@s,@z,1)
        ,@x=''
        ,@s=REPLACE(@s,left(@s,@z),'')
end
select @w
```
ex:
`exec x(@s='1337 numb3r5')`
returns `1333337 nuuumumumbumbumb3umb3rumb3rumb3r5`
[Answer]
# Python 2, 131 Bytes
```
i=raw_input();s=r='';g=1
for c in i:
 s+=c
 if g:r+=c;g=0;s=''
 elif c.isalpha():r+=s*2
 elif '/'<c<':':r+=s
 else:g=1;r+=c
print r
```
Loops through characters, and saves/repeats if required.
[Answer]
# Pip, 41 bytes
```
aR`[^_\W]+`{[[email protected]](/cdn-cgi/l/email-protection)@{1,++y}X2-(_Q+_)MJa}
```
Takes the text as a command-line argument. We use a regex replacement to replace all alphanumeric runs (`[^_\W]+`) with a callback function. This function maps another function to each character of the word, generating progressively larger runs of characters (with the global variable `y` keeping track of the index) and repeating them X2 if the character is not a number. (The logic for which uses `+` to convert to a number and then `Q` to check whether this is string-equal to the original character.) This process leaves out the first character, though, so we have to add it on afterward (`a@0.`).
[Answer]
# [Python 3](https://docs.python.org/3/), 86 bytes
```
f=0
m=''
for i in input():g=i.isalnum();m=m*g*f+i*f;print(end=m*-~i.isalpha()or i);f=g
```
[Try it online!](https://tio.run/##K6gsycjPM/7/P83WgCvXVl2dKy2/SCFTITMPiApKSzQ0rdJtM/UyixNz8kpzNTStc21ztdK10rQztdKsC4oy80o0UvNSgGK6dRBVBRmJGpogIzSt02zT//83NDY2VwBqTTIuMgUA "Python 3 – Try It Online")
]