Reputation: 117
I want to remove all the words, numbers, hexadecimal numbers after a specific word ".c:"
my line is like that-
line = "Bags has a price.c:123
line = "Bags has a price.c:543ea
I have tried with the following:
d = re.sub(r'[.c:\W+]', '', c)
But it not giving right answer, the output will be like:
output: Bags has a price
Upvotes: 3
Views: 1300
Reputation: 43832
And just using a simple index lookup.
>>> line = "Bags has a price.c:543ea"
>>> after_word = ".c"
>>> cleaned_line = line[:line.index(after_word) + len(after_word) ]
>>> cleaned_line
Bags has a price.c
To exclude .c
just remove the + len(after_word)
Upvotes: 0
Reputation: 624
If you have to use a regex — and clearly you don’t. You could have done this:
re.sub(r'\.c:.*?$','', line)
If you can avoid the regex, then do. It’ll likely be much slower that using split.
Upvotes: 0
Reputation: 14854
>>> line = "Bags has a price.c:123"
>>> line.split(':')[0]
'Bags has a price.c'
>>> line.split('.c')[0]
'Bags has a price'
Upvotes: 5
Reputation:
>>> line = "Bags has a price.c:123"
>>> ''.join(line.partition('.c')[:2])
'Bags has a price.c'
Upvotes: 2