regex - to trim a any substring inside bracket in the string -


i have string in python :

line=r"x:\folder\code\mod\accsc1c1.c 351: error -> warning 550 symbol xxx (line 34) not accessed" 

and want trim line remove (line 34). different case line number varies line may like:

 x:\accsc1c1.c 333: error -> warning 4' (line 536) not accessed x:\accsddc1.c 633: error -> warning 8' (line 111) not accessed 

so output should come like:

 x:\accsc1c1.c 333: error -> warning 4'  not accessed x:\accsddc1.c 633: error -> warning 8'  not accessed 

i used wildcard '*' not working eliminating brackets () showing errors , using re module.

thanks

try this:

import re line=r"x:\folder\code\mod\accsc1c1.c 351: error -> warning 550 symbol xxx (line 34) not accessed" re.sub("\(line \d+\)", '', line) 

'x:\folder\code\mod\accsc1c1.c 351: error -> warning 550 symbol xxx not accessed'

from documentation sub:

re.sub(pattern, repl, string, count=0, flags=0) return string obtained replacing leftmost non-overlapping occurrences of pattern in string replacement repl. if pattern isn’t found, string returned unchanged.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo