Python Forum

Full Version: Need your small favor
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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]", "")
>>> 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
(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.