Python Forum
regular expression and sub - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: regular expression and sub (/thread-22196.html)



regular expression and sub - yokaso - Nov-03-2019

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 ?


RE: regular expression and sub - Gribouillis - Nov-03-2019

Well, what does the python interpreter say?


RE: regular expression and sub - yokaso - Nov-03-2019

sorry i forgot ,and i just updated the post


RE: regular expression and sub - Gribouillis - Nov-03-2019

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.


RE: regular expression and sub - yokaso - Nov-03-2019

it's done and sorry


RE: regular expression and sub - Gribouillis - Nov-03-2019

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'...


RE: regular expression and sub - yokaso - Nov-03-2019

thank you, i thought that's took just 2 argument.
and i will change the match object.