I want to substitute numbers from 0-85. Right now i have 0-89. subject = re.sub(r'([0-8]\\w%?)', '', subjectext)
When I do subject = re.sub(r'([0-8][0-5]%?)', '', subjectext) The regex breaks.
Try a pattern like this:
[1-7]?[0-9]|8[0-5]
This will match any string consisting of an optional digit from 1 to 7 followed by a digit from 0 to 9 or an 8 followed by a digit from 0 to 5.
If you need to include an optional %
sign (which wasn't explicitly stated as a requirement, but your pattern seems to suggest that you might need it) use this:
([1-7]?[0-9]|8[0-5])%?
However, the above will match a string like 9
inside a larger string like 92
. If you don't want this, you might want to consider using lookarounds to make sure that the matched substring isn't preceded by or followed by another digit, like this:
(?<!\d)([1-7]?[0-9]|8[0-5])%?(?!\d)
这个正则表达式怎么样?
[1-7]?\d|8[0-5]
You want to get every number from 0 to 85, so every number start by 0-7 followed by 0-9 or 8 followed by 1, 2, 3, 4 or 5:
[1-7]?[0-9]|8[0-5]
# ^^^^^^^^^ ^^^^^^
0 to 79 | 80 to 85
Consider the following Regex...
([0-7][0-9]|8[0-5])
Good Luck!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.