Python Forum

Full Version: regular expression and sub
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
6
----> 7 case = re.sub(match,expression)
8
9 print(case)

TypeError: sub() missing 1 required positional argument: 'string'


can you help me to fix my code ?
Well, what does the python interpreter say?
sorry i forgot ,and i just updated the post
Please can you post the exact code that you're running with the exact error message. It is meaningless to use the error message of another piece of code.
it's done and sorry
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'...
thank you, i thought that's took just 2 argument.
and i will change the match object.