Chico3001 Chico3001 - 1 year ago 76
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?


Answer Source

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:


RegEx Demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download