Search code examples
python-3.xpython-re

How to remove both number and text from a parenthesis using regrex in python?


In the following text, I want to remove everything inside the parenthesis including number and string. I use the following syntax but I got result of 22701 instead of 2270. What would be a way to show 2270 only using re.sub? Thanks

import regex as re
import numpy as np
import pandas as pd

text = "2270 (1st xyz)"
text_new = re.sub(r"[a-zA-Z()\s]","",text)
text_new

Solution

  • Does the text always follow the same pattern? Try:

    import re
    import numpy as np
    import pandas as pd
    
    text = "2270 (1st xyz)"
    text_new = re.sub(r"\s\([^)]*\)","",text)
    print(text_new)
    

    Output:

    2270