I want to construct my own character class in a script, then modify (and use) it, for example:[:myclass:] contains a, *, \n (as linefeed) and [WHITESPACE].I want to add all characters to [:myclass:] from [:alnum:].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.
Character classes: construct my own
shell;text processing;regular expression
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:myclass='a*[:alnum:][:space:]'regexp=[$myclass]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.