regex - Regular Expression re.sub not detecting all matches -


i've been trying retrieve percentages text using regular expressions. sadly, using .sub not retrieve matches.

data = "tho grades of 2 students improved 5.2% , 6.2%." re_1 = re.compile(r"\b(\d+\.)?\d+(%|(\spercent))") data = re.sub(re_1, "__percentage__", data, re.i) 

i'm trying retrieve things such as: "5%","20.2%","5 percent","5.2 percent". word percent , percentage symbol being part of match fine suspect trouble comes overlap. when inputting above data current output is:

"the grades of 2 students improved __percentage__ , 6.2%." 

any tips on how make sure both percentages turn matches? lot.

ps: might relevant, i'm using python 3

the problem might having related way u handled in different python 3 versions. also, passing compiled regex object re.sub, while string pattern should passed first parameter.

import re p = re.compile(r'(\b\d+(?:\.\d+)?(?:\spercent|\%))') test_str = "the grades of 2 students improved 5.2% , 5.4% 5 percent or 1.2%." result = re.sub(p, "__percentage__", test_str) print (result) 

in ideone demo (uses python 3.4), code compiles , outputs

the grades of 2 students improved __percentage__ , __percentage__ __percentage__ or __percentage__. 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -