Question: Evening,

I want to search some columns in a MySQL table for any instances of [a-z]\.[a-z], for example:

John.than, Ame.ica, Llan.antffraid etc.

but I don't want this to include the strings 'a.m.' OR 'p.m.'. I have tried using (?!a.m.|p.m.) but this does not work. It returns the error: "Got error 'repetition-operator operand invalid' from regexp".

I have the following regular expression:

```
REGEXP BINARY '[a-z]\\\.[a-z]'

```

N.B. If a colum includes a.m. OR p.m. but also contains a string like bro.ken, it needs to be returned.

Answer: 
Build your regex step by step:

You want everything, except its a "standalone" a.m or p.m:

* `[b-oq-z]{1}\.[a-ln-z]{1}` matches everything of the format `x.y` that is not `a.#` or `p.#` or `#.m`

However you miss `a.a`, `a.b`, `a.c` ... also. so add that cases:

* `a\.[^m]` (same for the `p`-cases: `p\.[^m]`)

`a.m` is valid, when there are chars in front of the a: `kra.m, tra.m`. Same applies for `p.m`: `erp.m`

* `[a-z]{1}[ap]\.m` covers this condtion.

Now, we are missing strings, where the second part is longer: `a.mod, p.markt`:

* `[ap]\.m[a-z]+` covers that one.

Finally just the ones ending with `.m` but having a different prefix are missing:

* `[b-oq-z]{1}\.m`

This should now cover all possible use Cases. Simple combine the pattern with OR (`|`) and you are done:

```
([b-oq-z]{1}\.[a-ln-z]{1}|a\.[^m]|p\.[^m]|[a-z]{1}[ap]\.m|[ap]\.m[a-z]+|[b-oq-z]{1}\.m)

```

![Regular expression visualization](https://www.debuggex.com/i/MbZlQhM8TcmmnVO1.png)

[Edit live on Debuggex](http://www.debuggex.com/r/MbZlQhM8TcmmnVO1)

Note: This will NOT give you the exakt match groups. But since you use it in a SQL-Query only the case that *there is a match* is required. (`ark.m` will be matched by `k.m` - but it fulfills your specification)

Keep in Mind: When creating a regular expression, there is no *right* solution: Just Working Ones, and not working ones. `a\.[^m]|p\.[^m]` is equal to `[ap]\.[^m]`, which will reduce the pattern by one OR.

You have found the *perfect* Regex-Pattern, when 2 conditions are met:

* It works!
* You can understand it, when looking at it in 4 months!
If you can use assertions, this might work, but not sure about backtracking.

```
 #  (?=^.*(?:(?!a\.m|p\.m)[a-z]\.[a-z]|(?:a\.m|p\.m).*(?!a\.m|p\.m)[a-z]\.[a-z]))

 (?=
      ^
      .* 
      (?:
           (?! a\.m | p\.m )
           [a-z] \. [a-z] 
        |  
           (?: a\.m | p\.m )
           .* 
           (?! a\.m | p\.m )
           [a-z] \. [a-z]
      )
 )

```