I
I
i_ikigai2020-05-16 23:36:10
Python
i_ikigai, 2020-05-16 23:36:10

Is it possible to set a specific amount of replacement for re.sub?

This code replaces all suitable ones, is it possible to set a condition under which such an amount will be replaced as I write?
Given two lines. Delete the first occurrence of the second string in the first line

import re

a = 'asdasddasdcx'
b = 'as'
a = re.sub(b, '', a)
print(a)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-05-17
@i_ikigai

re.sub()

>>> import re
>>> pattern = re.compile('as')
>>> line = 'asdasddasdcx'
>>> re.sub(pattern, '', line, count=1)
'dasddasdcx'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question