Avoiding removing undesired characters inside a string prefix

  php, preg-replace, regex

I have a simple regex to remove undesired chars(like numbers for instance) from a string:

preg_replace(/[^a-z_]/, '', $str);

But now it must keep a prefix that contains some of those chars that were being removed. That prefix format consists of an alpha char followed by four numbers(prefix exemple: b1234). String example:

b7001_cp_parc_venc_fluxo

So i tried to add a non-capturing group for that prefix, but i can’t make it to work. Attempted to use things like:

(?:b[0-9]{4})[^a-z_]

But it stops removing numeric chars in the whole string, for example.

Source: Ask PHP

LEAVE A COMMENT