1

I have a string: 0220110000AL0091 and I would like to get back the last 000 for replace by three spaces. So for: 0220110000AL0091, I want to replace by 0220110 AL0091.

I don't know how to apply the regex between the 7th and 11th characters!

Thanks

Right leg
  • 16,080
  • 7
  • 48
  • 81
user2998243
  • 181
  • 1
  • 2
  • 12

1 Answers1

1

You're looking for a negative lookahead. You will look for a sequence of three zeros, not followed by a zero.

Here is how you could do it in Python (doc here, ctrl+f -> (?!):

>>> import re
>>> s = "0220110000AL0091"
>>> re.sub("000(?!0)", "   ", s)
'0220110   AL0091'
>>> 
Right leg
  • 16,080
  • 7
  • 48
  • 81
  • Thanks, it's perfect ! – user2998243 Mar 14 '18 at 14:16
  • 1
    @Rightleg how did you know OP is using python? – ctwheels Mar 14 '18 at 14:38
  • @ctwheels I don't, it's just an example with a common language that I know. Besides, as long as the language supports lookahead assumptions, this regex is likely to work, and if it didn't, I think the negative lookahead would still be the solution. – Right leg Mar 14 '18 at 14:41