Remove Characters After Number

I have a string from a file which I need to remove all the characters after the substring "c1525". Can regex be used for this? The pattern I am seeing is that all my strings have a "c" then 4 digits (although I have seen more than 4 digits, so need to take that into consideration).




>Solution :

You could use a regex with a capturing group and re.sub:

import re

s = 'd098746532d1234567c1525qFPXplFSm-FS8575664637338586224hKwHFmFSRRnm0Uc006566'

s2 = re.sub(r'(c\d{4,}).*', r'\1', s)

output: 'd098746532d1234567c1525'

Leave a Reply