Square brackets in a regular expression specify a character class -- it matches all the characters in the brackets.
Outside a character class, ^
matches the beginning of the string or beginning of a line (depending on whether the m
modifier is used). So the regular expression ^[a-z]+$
matches a line that is entirely letters. If you remove the $
(which matches the end of the string/line), you'll get a regexp that matches anything that starts with a letter; in that case, you also don't need the +
quantifier (anything that starts with 1 or more letters also starts with 1 letter).
At the beginning of a character class, ^
inverts the class. Instead of matching all the characters in the brackets, it matches all characters that are not in the brackets. So [^a-z]+$
matches anything that ends with non-letters, because $
matches the end of the string/line. As above, you don't need the +
, since anything that ends with 1 or more non-letters also ends with 1 non-letter. A regexp that matches when everything is a non-letter would be:
^[^a-z]+$
A regexp that matches when there's any non-letter in the line would be:
[^a-z]
i.e. no ^
or $
anchor.