Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to parse percentage from the string [duplicate]

I want to get all the percentage from above string with decimal or without decimal points. I also want to get % sign.

string  = """Bachelor of Engineering in Electronics from  in 2013 with 60%.
Diploma (Electronics & Video Engineering) from  in 2009 with 79.39%.
SSC from  in 2006 with 61.71%."""

I have tried as below

re.findall(r'\d+%|([0-9]\d?)\.\d+%', string)

getting output as

['', '79', '61']

But I want output as below

    ['60%', '79.39%', '61.71%']

Anybody can tell where I am going wrong and regex to get expected output.

like image 299
Akash Kinwad Avatar asked Mar 12 '26 17:03

Akash Kinwad


1 Answers

You can use the RegEx (\d+(\.\d+)?%)

  • \d+ captures 1 or more digits

  • (\.\d+)? captures a dot and 1 or more digits 0 or 1 time

  • % captures % literally

Demo.

like image 139
Zenoo Avatar answered Mar 14 '26 07:03

Zenoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!