# Golf a Sentence

Everybody loves golfing code. Code is written in English (eh...). Therefore, everybody loves golfing English!

Your task is to write a program or function that takes a string as input, and then returns or prints the golfed version. There will be no newlines or tabs in the input.

This is how you golf a string. The order matters!

1. Remove spaces directly before and after punctuation: "Hey there", he said ; he ' s a cat. -> "Hey there",he said;he's a cat. The following count as punctuation: .,?!"':;()

2. Replace he/she/it is, case insensitive, with he's/she's/it's: It is unfortunate that she is not coming ever. -> It's unfortunate that she's not coming ever.

3. Words that start with capital letters in a sentence can be moved against the preceding word (omitting the space): The farmer told Fred that he should leave. -> The farmer toldFred that he should leave.

4. Trailing and leading spaces should be removed: [ ]It's raining.[ ]-> It's raining. Ignore the brackets (annoying auto-formatting).

5. Remove repeated punctuation that is the same: ""Here''s the cake",, said Fred..! -> "Here's the cake",saidFred.!

If we were to golf the first paragraph of this challenge, the output would be:

Everybody loves golfing code.Code is written inEnglish(eh.).Therefore,everybody loves golfingEnglish!

This is code golf––or rather English golf––so shortest code in bytes wins!

Here is one all encompassing test case:

Input: " You are crazy"",, , , she said. It is unfortunate that Margaret had to say that... she is a Martian!.
Output: "You are crazy",she said.It's unfortunate thatMargaret had to say that.she's aMartian!.

• inEnglish(eh.). You collapsed the space between parentheses and text, though you don't explicitly count parens as punctuation. – Conor O'Brien Jul 8 '16 at 23:00
• Does "whitespace" mean all whitespace or just spaces? (i.e. does it include newlines, tabs, etc.) – Downgoat Jul 8 '16 at 23:05
• @Downgoat. Whitespace is just spaces. There are no new lines or tabs in the input. I'll clarify that in the question. – Daniel Jul 8 '16 at 23:07
• Why should the He is in He is here remain unchanged? – R. Kap Jul 8 '16 at 23:32
• @Dopapp Ok, well then, in that case, you should change the rule back to what it used to be. It makes no sense that She is and It is become She's and It's respectively, but He is does not become He's. – R. Kap Jul 8 '16 at 23:39

## Retina, 65 bytes

+ *([.,?!"':;()])\1* *| ([A-Z])| +$|^ +$1$2 i(s?he|it) is$1's

Regex replacement and then a simple trim

The magic regex is:

 *([.,?!"':;()])\1* *| ([A-Z])| +$|^ +  Try it online! # JavaScript ES6, 102926165 95 bytes x=>x.replace(/ *([.,?!"':;()])( *\1 *)*| ([A-Z])/g,"$1$3").replace(/(he|it) is/g,"$1's").trim()


Works on the last testcase. Saved a lot of bytes thanks to Downgoat! Impressive.

• x[R="replace"](/ *([.,?!"':;()]) */g,"$1")[R](/ ([A-Z])/g,"$1") -> x[R="replace"](/ *([.,?!"':;()]) *| ([A-Z])/g,"$1$2") – Downgoat Jul 8 '16 at 23:10
• @Downgoat I tried that earlier, but that didn't work for me XD works now tho – Conor O'Brien Jul 8 '16 at 23:11
• Make the first regex / *([.,?!"':;()])\1* *| ([A-Z])/g and the you should be able to remove the other prelace – Downgoat Jul 8 '16 at 23:12
• To fix bugs with hello, , , you're gonna have to make it: x=>x.replace(/ *([.,?!"':;()])( *\1 *)*| ([A-Z])/g,"$1$3").trim() at 65 bytes. I'll see if I can golf it more – Downgoat Jul 8 '16 at 23:22
• @Dopapp... in your console, maybe? – Conor O'Brien Jul 8 '16 at 23:23

# V, 77 bytes

Ó *¨[.,"':;?!()]© */±
Óó«úeõ
Ó^ *ü *\$


Way longer than I'd like.

Try it online!

• V is new retina? (can you add explanation) – Downgoat Jul 8 '16 at 23:02
• Eventually. This is a longer one so it might take a while to write out... – DJMcMayhem Jul 8 '16 at 23:02

# Python 3.5, 213 bytes:

import re;R=re.sub;W=lambda G:G.group(0);print(R(r'(\W)\1+',lambda f:W(f)[0],R('\s*[^\s\w]\s*|\s*[A-Z]',lambda f:W(f).strip(),R('((I|i)t|(s|S)?(h|H)e) is',lambda i:W(i)[:[*W(i)].index(" ")]+"'s",input().strip()))))


Uses 3 regular expressions in a row with re.sub in conjunction with Python's str.strip() built in to golf the input English sentence.

Try It Online! (repl.it)

# Bash 74 Sed 65 + 4 bytes

65 bytes for the script and 4 for the r and f flags.

sed -r "s/\s*([.,?\!\"\':;A-Z])(\1|\s)*/\1/g;s/(he|she|it) is/\1\'s/i" s/\s*([,.?\!\"\':;A-Z])(\1|\s)*/\1/g;s/(he|she|it) is/\1\'s/i

Usage: Enter text, press enter and then Ctrl + D (send EOF).