Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
regular expression and sub
#1
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 ?
Reply
#2
Well, what does the python interpreter say?
Reply
#3
sorry i forgot ,and i just updated the post
Reply
#4
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.
Reply
#5
it's done and sorry
Reply
#6
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'...
Reply
#7
thank you, i thought that's took just 2 argument.
and i will change the match object.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  data validation with specific regular expression shaheen07 0 296 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  Regular Expression search to comment lines of code Gman2233 5 1,591 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,601 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Need help with my code (regular expression) shailc 5 1,871 Apr-04-2022, 07:34 PM
Last Post: shailc
  Regular Expression for matching words xinyulon 1 2,132 Mar-09-2022, 10:34 PM
Last Post: snippsat
  regular expression question Skaperen 4 2,418 Aug-23-2021, 06:01 PM
Last Post: Skaperen
  How can I find all combinations with a regular expression? AlekseyPython 0 1,636 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  Python Regular expression, small sample works but not on file Acernz 5 2,858 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,364 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Regular expression: return string, not list Pavel_47 3 2,451 Jan-14-2021, 11:49 AM
Last Post: Pavel_47

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020