# Need help forming a regexp ("not" operator?)

**URL:** https://boards.straightdope.com/t/need-help-forming-a-regexp-not-operator/579736
**Category:** Factual Questions
**Created:** [April 26, 2011, 3:20am UTC](https://boards.straightdope.com/t/need-help-forming-a-regexp-not-operator/579736 "2011-04-26T03:20:38Z")
**Posts on this page:** 8
**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: [April 26, 2011, 3:20am UTC](https://boards.straightdope.com/t/need-help-forming-a-regexp-not-operator/579736/1 "2011-04-26T03:20:38Z")

</div>

I, unfortunately, never really learned regexps, and now I’m sort of in dire need of them. I could do this without them, but it would be a general PITA. I basically need to make a delimeter to match any characters NOT in a given set. \W gets close, but for the things I _don’t_ want to delimit I need to include a few special characters like apostrophes and hyphens, so something like --all the expressions NOT in “\w’-”–(that’s not all the characters necessary, but should give you an idea). You’d think it’d be easy to Google this, but it’s not. Maybe that’s because I don’t know how to use regexps…

---

<div class="post-metadata">

### Author: ![tellyworth](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@tellyworth](https://boards.straightdope.com/u/tellyworth)
#### Post date: [April 26, 2011, 3:23am UTC](https://boards.straightdope.com/t/need-help-forming-a-regexp-not-operator/579736/2 "2011-04-26T03:23:39Z")

</div>

[^abcd] means “match any character except a, b, c or d”.

So something like [^\w’-"–] should do what you want.

---

<div class="post-metadata">

### Author: ![yabob](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/yabob/32/2821_2.png) [@yabob](https://boards.straightdope.com/u/yabob)
#### Post date: [April 26, 2011, 3:27am UTC](https://boards.straightdope.com/t/need-help-forming-a-regexp-not-operator/579736/3 "2011-04-26T03:27:39Z")

</div>

If a character class delimited with square braces beings with a caret, it means match something not in the class:

[^,;:]

Matches a single character which is not a comma semicolon of colon, for instance. - may also be used to indicate ranges:

[^A-G1-5]

Matches a single character not an uppercase A through G or 1 through 5.

---

<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: [April 26, 2011, 3:33am UTC](https://boards.straightdope.com/t/need-help-forming-a-regexp-not-operator/579736/4 "2011-04-26T03:33:35Z")

</div>

Ah, I got confused because “^” seems to also be an anchor meaning “begins with.” What’s the difference between these usages? I assume it’s something like

^[A-G] means “starts with A-G”

while

[^A-G] mans “not A-G”

Is that right?

---

<div class="post-metadata">

### Author: ![yabob](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/yabob/32/2821_2.png) [@yabob](https://boards.straightdope.com/u/yabob)
#### Post date: [April 26, 2011, 3:50am UTC](https://boards.straightdope.com/t/need-help-forming-a-regexp-not-operator/579736/5 "2011-04-26T03:50:25Z")

</div>

> [@Jragon](#):
>
> Ah, I got confused because “^” seems to also be an anchor meaning “begins with.” What’s the difference between these usages? I assume it’s something like
> 
> \[1\] means “starts with A-G”
> 
> while
> 
> [^A-G] mans “not A-G”
> 
> Is that right?

Yep.

* * *

1. A-G

---

<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: [April 27, 2011, 5:48am UTC](https://boards.straightdope.com/t/need-help-forming-a-regexp-not-operator/579736/6 "2011-04-27T05:48:57Z")

</div>

So, I’m trying to make a delimiter that will separate paragraphs based on whether they have one or more blank lines separating them (in other words, 2 or more newline characters). I figured:

{2,}

Would work, when that failed, I tried

[  
]{2,}

Which also didn’t work. Am I missing something? Is the scanner just being buggy?

---

<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: [April 27, 2011, 6:02am UTC](https://boards.straightdope.com/t/need-help-forming-a-regexp-not-operator/579736/7 "2011-04-27T06:02:54Z")

</div>

Never mind, forgot Windows uses carriage returns. Stripping the file of carriage returns with a simple program (I only have to handle Unix-formatted files) worked perfectly.

---

<div class="post-metadata">

### Author: ![tellyworth](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@tellyworth](https://boards.straightdope.com/u/tellyworth)
#### Post date: [April 27, 2011, 6:06am UTC](https://boards.straightdope.com/t/need-help-forming-a-regexp-not-operator/579736/8 "2011-04-27T06:06:01Z")

</div>

Are you sure the input has just newlines (  
) and not crlfs (  
)?

ETA: never mind then.
