i would like to find a specific string and delete it from on initial string.
in my case i want to find the word "LOT N° 1" and delete if from the PHRASE
import re
expression ='LOT N° 1 that''s the first one'
match = re.compile(r'LOT N°.[1-9] ') ## i am making a pattern who match the word that i want to delete
case = re.sub(match,expression)
print(case)
the error message
Quote:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-a9c93ba1b0f0> in <module>
5 match = re.compile(r'LOT N°.[1-9] ') ## i am making a pattern who match the word that i want to delete
----> 7 case = re.sub(match,expression)
9 print(case)
TypeError: sub() missing 1 required positional argument: 'string'
can you help me to fix my code ?
re.sub() needs a second argument saying by what you want to replace the match in the initial string. Here you can use
case = re.sub(match, '', expression)
or
match.sub('', expression)
Also 'match' is not a very good name for a regex because it's often used for match objects which appear in this context. Better use 'pattern' or 'regex' or 'pat'...
List Creation and Position of Continue Statement In Regular Expression Code
new_coder_231013
1,424
Jun-15-2022, 12:00 PM
Last Post
:
new_coder_231013