SyntaxFix
Write A Post
Hire A Developer
Questions
Tags
I have some sample string. How can I replace first occurrence of this string in a longer string with empty string?
regex = re.compile('text') match = regex.match(url) if match: url = url.replace(regex, '')
This question is related to python regex
python
regex
Use re.sub directly, this allows you to specify a count:
re.sub
count
regex.sub('', url, 1)
(Note that the order of arguments is replacement, original not the opposite, as might be suspected.)
replacement
original
Source: Stackoverflow.com