Python Forum
Need your small favor
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need your small favor
#1
Hiii,

How can I remove "[inside some number], (Fig. 8))from the line?

I have tried by using below command but it is not working. please tell me another method for it ........

e.g. [18], (Fig. 8)

line= line.replace("[/0-9]", "")
Reply
#2
>>> import re
>>> p = re.compile(r"\[\d+\], \(Fig\. \d+\)")
>>> line = "matched: [11], (Fig. 8) unmatched: [1], Fig. 9, matched: [1234], (Fig. 17), unmatched: [7], (Fig: 33)"
>>> re.sub(p, "", line)
'matched:  unmatched: [1], Fig. 9, matched: , unmatched: [7], (Fig: 33)'
if it should be only Fig. 8, replace second \d+ with 8
Reply
#3
(Mar-21-2017, 04:00 AM)desul Wrote: Hiii,

How can I remove "[inside some number], (Fig. 8))from the line?

I have tried by using below command but it is not working. please tell me another method for it ........

e.g. [18], (Fig. 8)

line= line.replace("[/0-9]", "")

Your code is removing the string "[/0-9]".  Per your example, that should never be in the source string.
It looks like you're trying to write a regular expression, which is what zivoni demonstrated.  For testing, I like regexpal (http://www.regexpal.com/), since regular expressions can quickly get complicated.
Reply


Forum Jump:

User Panel Messages

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