Your task is to create a program which takes a string, and if it contains any numbers, symbols or other non-letter characters, it throws an error. Else, it returns the original string. (The string may include whitespace)

("Letters" are a-z and A-Z)


Input: My name is Joe

Output My name is Joe

Input: My username is Joe32

Output: (insert an error)

(FYI this question has nothing to do with Don't Google google. It was identified as a possible copy.)

  • \$\begingroup\$ Can we read the string from stdin? \$\endgroup\$ Sep 30, 2015 at 15:48
  • 2
    \$\begingroup\$ possible duplicate of Don't google "google" \$\endgroup\$
    – mbomb007
    Sep 30, 2015 at 16:05
  • 3
    \$\begingroup\$ When you way "whitespace," do you mean any whitespace character, including tabs and newlines, or only spaces? \$\endgroup\$
    – Alex A.
    Sep 30, 2015 at 16:08
  • 2
    \$\begingroup\$ In fact, there are even more whitespace characters in the ASCII range namely vertical tabs, carriage returns and form feeds. \$\endgroup\$ Sep 30, 2015 at 16:39
  • 1
    \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ Except, I can't think of a way to make it throw an error based on the input. :/ \$\endgroup\$ Sep 30, 2015 at 17:05

17 Answers 17


Julia, 53 bytes


Read a string from STDIN using readline(). If any of its characters are not letters or whitespace (ismatch(r"[^a-z\s]"i, s)) then we error(), otherwise we print(s).


Gema, 14 characters


Sample run:

bash-4.3$ gema '\W<-L>=@div{;}' <<< 'My name is Joe'
My name is Joe

bash-4.3$ gema '\W<-L>=@div{;}' <<< 'My username is Joe32'
Floating point exception (core dumped)

PowerShell, 24 bytes

Filter the pipeline input based on whether it matches the regex:


e.g. try it by piping the string in:

PS C:\> "Test Test"|?{$_-match'^[a-z\s]+$'}
Test Test

PS C:\> "Test ! Test"|?{$_-match'^[a-z\s]+$'}

The error message is silence/empty result. It matches case-insensitively by default.

  • \$\begingroup\$ Empty result isn't really an error, though? I came up with param($a)if($a-replace'[a-z\s]',''){1/0;exit}$a for 47 bytes. \$\endgroup\$ Sep 30, 2015 at 16:26

C++14, 132 126 121 117 121 bytes

main(){std::string s;getline(std::cin,s);for(char c:s)if(!isalpha(c)&!isspace(c))throw;std::cout<<s;}

We read a line from stdin, check if each character is a letter or space - if it's not, we throw an error. Finally outputs the input.

Thanks to Alex A for golfing off two bytes

  • \$\begingroup\$ Isn't std::getline defined in <string> header? Also, I think it is C++11 (I am not saying it is not C++14 though) \$\endgroup\$
    – Zereges
    Sep 30, 2015 at 16:14
  • \$\begingroup\$ std::getline isn't defined in <string>. Yes, it probably is - but I'm compiling it in C++14 so I find it safer to write it that way. Thanks anyway :~)! Actually, regular getline is defined in iostream which saves me 5 bytes! \$\endgroup\$ Sep 30, 2015 at 16:20

STATA, 57 bytes

di _r(a)
gl b=1/regexm("$a","^[a-zA-Z ]*$")
di _d($b)"$a"

Uses a regex to match letters and space (STATA's regex doesn't include character classes). Set a variable to 1/match exists. Then it prints a that many times, if match exists=0, it says it's invalid syntax. Otherwise, it prints it 1/1 = 1 time.


CJam, 18 bytes


The multi-line string contains a tabulator, a space and a linefeed.

Try it online in the CJam interpreter.

How it works

q_                    Read all input from STDIN and push a copy.
  "\t \n"             Push that string.
         '[,_el^      Push all letters. See: codegolf.stackexchange.com/a/54348
                |     Concatenate both strings.
                 -    Remove their characters from the copy of the input.
                  !   Logical NOT. Pushes 0 if there are other characters.
                   /  Split. Doesn't affect output if successful. Errors on 0.

Python 3, 56 characters

import re;print(re.match(r'[a-z\s]+$',input(),2).string)

Usefully, match objects have this attribute string which is

The string passed to match() or search().

while None does not.

In [10]: import re;print(re.match(r'[a-z\s]+$',input(),2).string)
AttributeError                            Traceback (most recent call last)
<ipython-input-10-0851dec671d1> in <module>()
----> 1 import re;print(re.match(r'[a-z\s]+$',input(),2).string)

AttributeError: 'NoneType' object has no attribute 'string'

In [11]: import re;print(re.match(r'[a-z\s]+$',input(),2).string)

Haskell, 56 bytes

q x|all(`elem`(['a'..'z']++['A'..'Z']++['\t'..'\r']))x=x

Throws the Non-exhaustive patterns in function q error if the string contains forbidden characters.


JavaScript, 48 46 45 46 31 Bytes


Accepts input, searches for invalid characters, if it finds any, returns a truthy value and thus evaluates the undefined character q. Otherwise, returns null and thus returns the string.

Old version, 44 bytes


Tries to evaluate the invalid character @ upon finding a numeric.

Edit 1: -2 bytes, thanks to @Alex A.

Edit 2: -1 byte, reworked. BONUS: Accounts for ALL whitespace! ^_^

Edit 3: +1 byte, added underscore testing.

Edit 4: Back to 46, unreworked.

Edit 5: -2 extra bytes to @edc65 (for old version; -1 for new).

  • \$\begingroup\$ Can you use a regex option to make it case insensitive? I don't know the JS regex flavor but you may be able to do /[^a-z ]/gi to save 2 bytes. \$\endgroup\$
    – Alex A.
    Sep 30, 2015 at 16:30
  • \$\begingroup\$ Per the OP's comment, looks like we need to account for all whitespace characters, not just spaces. For that you may be able use /[^a-z\s]/gi. \$\endgroup\$
    – Alex A.
    Sep 30, 2015 at 16:41
  • \$\begingroup\$ From the OP's second example, it looks like we are supposed to throw the error if numbers are in the string. \$\endgroup\$
    – Celeo
    Sep 30, 2015 at 17:04
  • \$\begingroup\$ @AlexA. Yeah; by DeMorgan's laws, the negation of a disjunction (the regular expression) results in a conjuction of the negatives of the expression. Input the expression into RegExr.com to get a visual of the matched expressions. \$\endgroup\$ Sep 30, 2015 at 20:43
  • \$\begingroup\$ @edc65 I don't know why your comment was upvoted. You are wrong in your statement. \$\endgroup\$ Sep 30, 2015 at 20:49

Python 2, 60 bytes

import re;n=input();print[n][re.sub('[^a-zA-Z\s]+','',n)!=n]


$ python -c "import re;n=input();print[n][re.sub('[^a-zA-Z\s]+','',n)!=n]"

Hassium, 166 Bytes

func main(){r="";foreach(c in input()){c=c.toString();if(!("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ \t\n\r".contains(c)))throw(c);else r+=c;}println(r);}

Run and see expanded here


PHP, 51 48 bytes


Runs from command line, like:

php test.php "Test me"

Using preg_match is shorter than using ctype_alpha as the latter needs whitespaces to be stripped.

As the error wasn't defined in the challenge, calling an undefined function seems the shortest way to generate an error. So this:

php test.php "Test me, 2"

leads to:

PHP Fatal error: Call to undefined function e()


Saved 3 bytes by inverting the statement an by using the i-modifier. Thanks to fschmengler.

  • \$\begingroup\$ You can save a few bytes with the case insensitive modifier i instead of a-zA-Z. And even more if you negate the expression: <?=preg_match('/[^a-z\s]/i',$a=$argv[1])?e():$a; (it searches for any character that is not a white space or alphabetic character) I replaced `` with \s because the question said, all whitespaces are allowed. \$\endgroup\$ Oct 4, 2015 at 18:43
  • \$\begingroup\$ @fschmengler Thanks a lot - I've updated the answer. \$\endgroup\$ Oct 9, 2015 at 7:05

Stax, 8 bytes


Run and debug online!

Shortest solution so far but expect to be defeated soon.


Uses the unpacked version to explain.

' Vl+-!uy
' Vl+        Alphabet plus space
     -       Remove from input
      !u     If result is not empty raise division by zero error
        y    Else print input

Python 2, 68 71 70 69 bytes

import re;s=raw_input();print s if re.match(r'[a-z\s]+$',s,2)else 0/0

Thanks to Alex A. and Martin Büttner for saving a byte and NightShadeQueen for another byte!

  • \$\begingroup\$ You should be able to shorten your regex by removing the position anchors and the +. You could also take out one of A-Z or a-z if there's an easy way to make it a case-insensitive regex. \$\endgroup\$
    – Alex A.
    Sep 30, 2015 at 17:09
  • 1
    \$\begingroup\$ @AlexA. The + has to stay, because the regex ensures that the string consists only of those characters. But I think the anchors might be unnecessary, because IIRC match checks against the entire string anyway (scan looking for substrings instead). \$\endgroup\$ Sep 30, 2015 at 17:13
  • \$\begingroup\$ Thanks both. I was able to remove the beginning anchor as yes, match automatically searches from the start of the string. \$\endgroup\$
    – Celeo
    Sep 30, 2015 at 17:18
  • \$\begingroup\$ @AlexA. It looks like I can make the regex case-insensitive with re.match(r'[a-z\s]+$',s,re.I) but it's more bytes than the case-sensitive expression. Thanks though! \$\endgroup\$
    – Celeo
    Sep 30, 2015 at 17:22
  • 1
    \$\begingroup\$ re.I is just 2. You could do re.match(r'[a-z\s]+$',s,2) \$\endgroup\$ Oct 1, 2015 at 20:58

Java 127 Bytes

class A{public static void main(String[]a){String s=System.console().readLine();System.out.print(s.matches("[A-z ]+")?s:0/0);}}

In Java from the command line you can have input and with the help of regular expression [A-z ]+ which matches alphabets and space in the line, you can have this kind of trick.Moreover, 0/0 will throw ArithmeticException.


C# - 174 157 148 bytes

using System.Linq;namespace System{class A{static void Main(string[]s){if(!s[0].All(char.IsLetter))throw new Exception();Console.WriteLine(s[0]);}}}


Console application that takes a parameter in the form of a string and checks via LINQ if all the characters in that string are letters. If so, writes it to console. Otherwise throws exception.

Not particularly short. Will keep tinkering!

Edit: Think I'm pretty much done. Can't get it much shorter don't think.


Ruby, 26 25 bytes

"undefined method `+@' for nil:NilClass (NoMethodError)":


Previous 26 bytes, "in `/': divided by 0 (ZeroDivisionError)":

puts gets[/^[A-z]*$/]||1/0

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