# Any way to define a character class "except" in POSIX Regexps?

**URL:** https://boards.straightdope.com/t/any-way-to-define-a-character-class-except-in-posix-regexps/595084
**Category:** Factual Questions
**Created:** [September 3, 2011, 6:26pm UTC](https://boards.straightdope.com/t/any-way-to-define-a-character-class-except-in-posix-regexps/595084 "2011-09-03T18:26:43Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)
#### Post date: [September 3, 2011, 6:26pm UTC](https://boards.straightdope.com/t/any-way-to-define-a-character-class-except-in-posix-regexps/595084/1 "2011-09-03T18:26:43Z")

</div>

I want to match, basically, “any printable character except \<whatever\>.” Whatever changes depending on exactly what I’m matching (for instance, any printable except ‘\<’). Is there any way to specify [:print:], except ‘\<’? I tried [[:print:]^\<], but it didn’t seem to work correctly. Is there any way to do this, or do I just have to specify the range of printables except that character manually?

---

<div class="post-metadata">

### Author: ![JWT\_Kottekoe](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@JWT\_Kottekoe](https://boards.straightdope.com/u/JWT_Kottekoe)
#### Post date: [September 3, 2011, 6:56pm UTC](https://boards.straightdope.com/t/any-way-to-define-a-character-class-except-in-posix-regexps/595084/2 "2011-09-03T18:56:18Z")

</div>

I’m not sure about POSIX, but in many flavors [^xyz] means any character other than x, y, or z.

Edited: Never mind, I didn’t understand your original question because of the smileys.

---

<div class="post-metadata">

### Author: ![JWT\_Kottekoe](https://avatars.discourse-cdn.com/v4/letter/j/3ec8ea/32.png) [@JWT\_Kottekoe](https://boards.straightdope.com/u/JWT_Kottekoe)
#### Post date: [September 3, 2011, 7:06pm UTC](https://boards.straightdope.com/t/any-way-to-define-a-character-class-except-in-posix-regexps/595084/3 "2011-09-03T19:06:05Z")

</div>

I don’t know how to do this in POSIX. With .Net regular expressions, you can use character class subtraction, so for example [\w-[ab]] means any alphanumeric except “a” or “b”. I don’t see this syntax in a cheat sheet for POSIX. You should be able to do it using a lookahead assertion.

---

<div class="post-metadata">

### Author: ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)
#### Post date: [September 3, 2011, 7:10pm UTC](https://boards.straightdope.com/t/any-way-to-define-a-character-class-except-in-posix-regexps/595084/4 "2011-09-03T19:10:03Z")

</div>

> [@JWT\_Kottekoe](#):
>
> I’m not sure about POSIX, but in many flavors [^xyz] means any character other than x, y, or z.
> 
> Edited: Never mind, I didn’t understand your original question because of the smileys.

Yeah, sorry forgot [noparse]:p[/noparse] -\> :p, basically I’m asking (if we’re using PERL) if there’s a way to say (using alnum this time):

\w except “m.” Just a way to take an already constructed character class and REMOVE an element (or handful of elements) from it, rather than building it from the ground up. The only real difference I’ve noticed between PERL and POSIX regexps is way you specify the default character classes, the syntax is almost the same.

Hmm, maybe I will have to use lookahead. A pain, but I guess I have to work with it… damn flex/bison.

---

<div class="post-metadata">

### Author: ![Manduck](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/manduck/32/256_2.png) [@Manduck](https://boards.straightdope.com/u/Manduck)
#### Post date: [September 3, 2011, 7:30pm UTC](https://boards.straightdope.com/t/any-way-to-define-a-character-class-except-in-posix-regexps/595084/5 "2011-09-03T19:30:25Z")

</div>

You could just do [A-LN-Za-ln-z0-9\_]

---

<div class="post-metadata">

### Author: ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)
#### Post date: [September 3, 2011, 7:52pm UTC](https://boards.straightdope.com/t/any-way-to-define-a-character-class-except-in-posix-regexps/595084/6 "2011-09-03T19:52:02Z")

</div>

> [@Manduck](#):
>
> You could just do [A-LN-Za-ln-z0-9\_]

Oh, I know, I was just hoping for something prettier and more self explanatory looking.

---

<div class="post-metadata">

### Author: ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)
#### Post date: [September 4, 2011, 6:36am UTC](https://boards.straightdope.com/t/any-way-to-define-a-character-class-except-in-posix-regexps/595084/7 "2011-09-04T06:36:08Z")

</div>

Using a negative lookahead, you could try:

```auto

(?!m)\w

```

---

<div class="post-metadata">

### Author: ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)
#### Post date: [September 4, 2011, 7:21am UTC](https://boards.straightdope.com/t/any-way-to-define-a-character-class-except-in-posix-regexps/595084/8 "2011-09-04T07:21:46Z")

</div>

It looks like within the flex/bison package itself there’s support for a subtraction syntax unlike standard POSIX style regexps, so {a-c}-{b-z} comes out as “a”. Oddly, instead of using square brackets (like you do for, oh, every other character class), you use curly brackets… for some reason.

I discovered this while poring over the manual for something completely unrelated, in a completely unrelated section.

---

<div class="post-metadata">

### Author: ![robert\_columbia](https://avatars.discourse-cdn.com/v4/letter/r/e79b87/32.png) [@robert\_columbia](https://boards.straightdope.com/u/robert_columbia)
#### Post date: [September 4, 2011, 1:00pm UTC](https://boards.straightdope.com/t/any-way-to-define-a-character-class-except-in-posix-regexps/595084/9 "2011-09-04T13:00:56Z")

</div>

or, you could just declare one with an exhaustive list of every printable character except \<whatever\>. Ugly, sure. But if it has to be done by lunch, then it might be worth it.

There are only finitely many characters.

---

<div class="post-metadata">

### Author: ![Manduck](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/manduck/32/256_2.png) [@Manduck](https://boards.straightdope.com/u/Manduck)
#### Post date: [September 6, 2011, 4:19am UTC](https://boards.straightdope.com/t/any-way-to-define-a-character-class-except-in-posix-regexps/595084/10 "2011-09-06T04:19:00Z")

</div>

Can you do it in two steps? First allow every character in the class, then run the result through another regexp that disallows the characters you don’t want. My perl is rusty, but in a shell script I might do something like this:

grep \w | grep -v m
