0

I have a query that searches text using a regex:

$filter = ['word' => new MongoDB\BSON\Regex("some text","i")];
$query = new MongoDB\Driver\Query($filter);

I now want to be able to search for text that does not contain the text: e.g. [^(some text)]

The first issue I'm having is that I can't get the NOT to work, it appears that ^ can only be used to force the start of the string. Here is what I tried to find text without the letter 'a' in:

$pat = '[^a]';
$filter = ['word' => new MongoDB\BSON\Regex($pat,"i")];
$query = new MongoDB\Driver\Query($filter);

That just returns all documents with a value set for 'word'

I also tried:

$pat = '^a';

But as expected, that returns all documents that start with an 'a'.

How can I get this to work?

Filnor
  • 1,290
  • 2
  • 23
  • 28
Kerrin Hardy
  • 31
  • 1
  • 8
  • `[^a]` just means find me a _single character_ that is anything but an `a`, _anywhere_ in the test string. So f.e. `foobar` would match that perfectly fine - because it has multiple characters that are not `a`. You would have to anchor this at the beginning an end, using ^ and $, and then look for multiple characters that match this class, if you want to find all texts that do not have `a` in them at all: `^[^a]+$` – CBroe Aug 24 '17 at 09:11
  • Oh wow. I used to be good a RegEx, I guess I need to use it more often. That was the issue. `^[^a]+$` fixes it. – Kerrin Hardy Aug 24 '17 at 09:15
  • Try `^(?!.*some text).+$` ;). – shA.t Aug 24 '17 at 09:27

0 Answers0