egrep: character class syntax is [[:space:]], not [:space:]


Writing complex regex expressions can be daunting. And the task is sometimes exacerbated by strange error messages that you get from the tools. I was tweaking the configuration for logcheck and suddenly started to get this error:

egrep: character class syntax is [[:space:]], not [:space:]

Which is really not providing that much context and therefore very unhelpful. Not to mention that in this case [:space:] may mean any of [:alnum:], [:alpha:], [:digit:], [:space:], [:word:] and others. The error message is exactly the same whatever character class produced the error. So you know there is a problem somewhere but you don’t even know with which class.

The typical problem causing this sort of message is that when you write complex expressions you forget to enclose the class into square brackets. The square brackets are required, for example, for operators like ‘+’. So a correct expression would look like this:

[[:alnum:]]+ [[:digit:]]+

But it is easy to overlook a mistake when you write this:

[:alnum:]+ [:digit:]+

It still kinda looks okay because the square brackets are there but you are missing the extra pair and it is hard to spot. So it may be helpful to look for patterns like ‘:\][?+*]’ in your code with grep or vim search.


Leave a Reply

Your email address will not be published. Required fields are marked *