Take first some character of string but if particular character will come consider that character also

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]

Leave a Comment