I\'m trying to extract the number before character \"M\" in a series of strings. The strings may look like:
\"107S33M15H\"
\"33M100S\"
\"12M100H33M\"
You can use rpartition to achieve that job.
s = '107S33M15H'
prefix = s.rpartition('M')[0]
You may use a simple (\d+)M
regex (1+ digit(s) followed with M
where the digits are captured into a capture group) with re.findall
.
See IDEONE demo:
import re
s = "107S33M15H\n33M100S\n12M100H33M"
print(re.findall(r"(\d+)M", s))
And here is a regex demo