Chico3001 Chico3001 - 2 months ago 10
PHP Question

php regex not ending with 'C'

I can get the correct regex to find all the strings that contain "TB" + 8 numbers and doesnt end with "C"

This should match: TB12345678
This shouldn't match TB12345678C

My PHP code is:

$this->tbTrabajadores = preg_grep('/^TB([0-9]{8})[^C]/', $this->tablas);


Can anyone help me?

Thanks!!!

Answer

You can use alternation in the end:

$this->tbTrabajadores = preg_grep('/^TB\d{8}([^C]|$)/', $this->tablas);

([^C]|$) will match either a non-C char or line end.

You can also use a negative lookahead after matching 8 digits:

/^TB\d{8}(?!C)/

RegEx Demo