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).
d098746532d1234567c1525qFPXplFSm-FS8575664637338586224hKwHFmFSRRnm0Uc006566
expected:
d098746532d1234567c1525
CodePudding user response:
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'
CodePudding user response:
You could use str.partition
join
:
out = ''.join(s.partition('c1525')[:2])
Output:
'd098746532d1234567c1525'
CodePudding user response:
s = 'd098746532d1234567c1525qFPXplFSm-FS8575664637338586224hKwHFmFSRRnm0Uc006566'
s[:s.find('c1525') len('c1525')]
Output:
d098746532d1234567c1525
CodePudding user response:
>>> txt = "d098746532d1234567c1525qFPXplFSm-FS8575664637338586224hKwHFmFSRRnm0Uc006566"
>>> import re
>>> re.match("[^c]*(c\d )", txt).group()
'd098746532d1234567c1525'