Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to construct my own character class in a script, then modify (and use) it, for example:

  1. [:myclass:] contains a, *, \n (as linefeed) and [WHITESPACE].
  2. I want to add all characters to [:myclass:] from [:alnum:].
  3. I want to remove the p character and the entire [:upper:] class from [:myclass:]

The preferred solution doesn't depend on scripting languages. If you want to, use bash.

Bonus question: how to store this systemwide? Whenever I boot, [:myclass:] has to be available.

share|improve this question
I don't know very much about this but you could probably accomplish this with a custom LC_CTYPE setting. It may require defining a custom locale. – jw013 Mar 11 '12 at 23:46
up vote 2 down vote accepted

I'm afraid that the list of character classes is hard-coded in the C library (e.g. in GNU libc, in the build_charclass function in posix/regcomp.c). The only way to extend it would be to recompile the C library.

You can customize the contents of each existing class in a locale definition.

In most cases, it should be good enough to build your regexp as a string:


You can't subtract characters from a category this way. And take care if adding ] or - or \ to respect the syntax of character classes in your language's regexes.

share|improve this answer
Excellent solution of you - as always. :) I think I will try both of them, but the second seems way more easier. I could even add a MYCLASS environment variable to the init scripts. – vakufo Mar 12 '12 at 5:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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