I wanna know why I don't get a result in this case:

echo "This doesn't work." | grep -E '[[:upper:]]([[:upper:]] | [[:lower:]])*\.'

I'm learning regex in Grep and what I'm trying to do here is detect whether the input is a sentence or not. For that I'm using grep with the extended option and trying to match any input that starts with an uppercase letter and is then followed by any number of uppercase and lowercase letters and ends with a period. The thing is that the input doesn't get matched and I can't understand why.

Here's the working example:

echo "This works." | grep -E '[[:upper:]][[:upper:][:lower:] ]*\.'

Also why do we need? the space after [:lower:] in the second bracket expansion before closing it? Why is that required for it to work?

2 Answers 2


There's basically two "errors" in your expression. First, you don't allow for matching the '. Secondly, ([[:upper:]] | [[:lower:]])* will match strings like A A   a a, i.e. sequences of capital letters followed by a space, or lowercase letters preceded by space.

What you need for matching the string This doesn't work., with explicit matching of the uppercase T and the dot, and something that matches uppercase, lowercase, space and ' in-between:

$ echo "This doesn't work." | grep -E "[[:upper:]]([[:upper:]]| |'|[[:lower:]])*\."
This doesn't work.

This is better written as

$ echo "This doesn't work." | grep "[[:upper:]][[:upper:][:lower:]' ]*\."
This doesn't work.

(even though the second [:upper:] in not actually triggered by the test string in this example; we could have used [[:upper:]][[:lower:]' ]*\.)

The space inside the bracketed expression allows for matching an actual space in the text. Without that space, the pattern would not match (you have two spaces in the test string). This is also why I include the ' character there, because you obviously have texts that you want to match that contains these.


Your first example:

You are looking for an uppercase character, followed by any number of upper or lowercase characters repeating zero to unlimited times. However you sentence contains whitespace and punctuation ('). Regex is pretty powerful but I don't think it's the right tool for what you are trying to achieve. For starters sentences can end with all sorts of punctuation. Also there really isn't any limit to the characters that a sentence can contain.

I believe it was Stéphane that said you should always use `--` with rm!

Wouldn't that be a sentence?

The reason you need the space in the second example is the same reason your first example doesn't work...there is a space in your sentence.

You could possibly use something like this:


However it's just matching anything that starts with an uppercase (English) character and ends with punctuation with no real regard for anything in between.

Additionally what about a sentence like the following?

iTunes is my least favorite music player.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .