Just see if that substring is in there. If it is, shift position
up until it’s past that substring:
x = 'color id [0-9]* with [0-9]*[\s]'
substring = '[0-9]*'
position = 23
length = len(substring)
index = x.find(substring, position - length)
if position - length < index < position + length - 1:
position = index + length
print x[:position]