Python Forum
Need your small favor - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Need your small favor (/thread-2481.html)



Need your small favor - desul - Mar-21-2017

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]", "")



RE: Need your small favor - zivoni - Mar-21-2017

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


RE: Need your small favor - nilamo - Mar-22-2017

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