to trim a any substring inside bracket in the string
Question: I have string in python :

    
    
    line=r"X:\folder\Code\Mod\ACCSC1C1.c 351: Error -> Warning 550 Symbol XXX (line 34) not accessed"
    

and I want to trim this line like to remove (line 34). But for different case
line number varies like line may be like:

    
    
    X:\ACCSC1C1.c 333: Error -> Warning 4' (line 536) not accessed
    X:\ACCSddC1.c 633: Error -> Warning 8' (line 111) not accessed
    

so my 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 '*' but it is not working even eliminating brackets () showing
some errors , usually am using re module.

Thanks


Answer: 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 the documentation for `sub`:

> re.sub(pattern, repl, string, count=0, flags=0) Return the string obtained
> by replacing the leftmost non-overlapping occurrences of pattern in string
> by the replacement repl. If the pattern isn’t found, string is returned
> unchanged.

