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!.

  • \$\begingroup\$ inEnglish(eh.). You collapsed the space between parentheses and text, though you don't explicitly count parens as punctuation. \$\endgroup\$ Jul 8, 2016 at 23:00
  • \$\begingroup\$ Does "whitespace" mean all whitespace or just spaces? (i.e. does it include newlines, tabs, etc.) \$\endgroup\$
    – Downgoat
    Jul 8, 2016 at 23:05
  • \$\begingroup\$ @Downgoat. Whitespace is just spaces. There are no new lines or tabs in the input. I'll clarify that in the question. \$\endgroup\$
    – Daniel
    Jul 8, 2016 at 23:07
  • 1
    \$\begingroup\$ Why should the He is in He is here remain unchanged? \$\endgroup\$
    – R. Kap
    Jul 8, 2016 at 23:32
  • \$\begingroup\$ @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. \$\endgroup\$
    – R. Kap
    Jul 8, 2016 at 23:39

5 Answers 5


Retina, 65 bytes

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

Regex replacement and then a simple trim

The magic regex is:

 *([.,?!"':;()])\1* *| ([A-Z])| +$|^ +

Try it online!


V, 77 bytes

Ó *¨[.,"':;?!()]© */±
Óã¨s¿heüit©ús is/'s
Ó^ *ü *$

Way longer than I'd like.

Try it online!

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

JavaScript ES6, 102 92 61 65 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.

  • \$\begingroup\$ x[R="replace"](/ *([.,?!"':;()]) */g,"$1")[R](/ ([A-Z])/g,"$1") -> x[R="replace"](/ *([.,?!"':;()]) *| ([A-Z])/g,"$1$2") \$\endgroup\$
    – Downgoat
    Jul 8, 2016 at 23:10
  • \$\begingroup\$ @Downgoat I tried that earlier, but that didn't work for me XD works now tho \$\endgroup\$ Jul 8, 2016 at 23:11
  • \$\begingroup\$ Make the first regex / *([.,?!"':;()])\1* *| ([A-Z])/g and the you should be able to remove the other prelace \$\endgroup\$
    – Downgoat
    Jul 8, 2016 at 23:12
  • \$\begingroup\$ 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 \$\endgroup\$
    – Downgoat
    Jul 8, 2016 at 23:22
  • \$\begingroup\$ @Dopapp... in your console, maybe? \$\endgroup\$ Jul 8, 2016 at 23:23

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).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.