Roger Roger - 6 months ago 14
PHP Question

Regex to remove words with more than 2 digits

I've got a list of product names with codes with 3 or more digits and some letters in it. Example: XZ11A8, G123T5M and 194FF4. Now I want to remove these kind of words with a regex. As a beginner in regex I came up with this:

preg_replace('/\d{3}+/', '', 'Product X XZ11A8');

Unfortunately this only works when the three digits are right next to each other. What is the right regex to always remove a word with 3 or more digits?


This pattern might work for you:


Test implemented in Python:

import re

pattern = r'\b([A-Z]*\d[A-Z]*){3,}\b'

string1 = 'Product X XZ11A8'
string2 = 'Example: XZ11A8, G123T5M and 194FF4.'

print(re.sub(pattern, '', string1, flags=re.IGNORECASE))

print(re.sub(pattern, '', string2, flags=re.IGNORECASE))


Product X 
Example: ,  and .

The assumption here is that what you want to remove is always alphanumeric (or underscore) and nothing else. E.g. if characters like hyphens get involved, then \b might fail us in this situation.