Assuming I have the following string:
thestring = "1) My Favorite Pokemon Charizard *22.00 MP* [Pre-Avatar Mode Cost: 15.75 MP] [Post-Avatar Mode Cost: 6.250 MP]"
Some other samples could be:
thestring = "1) My Favorite Pokemon Mew *1 MP* [Pre-Avatar Mode Cost: 0.5 MP] [Post-Avatar Mode Cost: 0.5 MP]"
thestring = "1) My Favorite Pokemon Pikachu *6.25 MP* [Pre-Avatar Mode Cost: 5 MP]; [Post-Avatar Mode Cost: 1.25 MP]"
(colon for the third case is intentional)
How to best extract the values of “Pre-Casting Cost” and “Post-Avatar Mode Cost”? I hear regex, but also string.find methods, but am not sure what is the best way to accomplish this. Note that there though the “Pre-Avatar Mode Cost” may be 15.75 MP, but could also depending on variety, could also be 15.752 or contain multiple decimal places. Syntax is appreciated.
UPDATE:
I am using Python 2.7. Closest answer is the following:
m = re.match('\[Pre-Avatar Mode Cost: (?P<precost>\d(\.\d*){0,1}) MP\] \[Post-Avatar Mode Cost: (?P<postcost>\d(\.\d*){0,1}) MP\]', '1) My Favorite Pokemon Mew *1 MP* [Pre-Avatar Mode Cost: 0.5 MP] [Post-Avatar Mode Cost: 0.5 MP]')
Though it appears to not actually match properly resulting in m results in a “Nonetype”due to no matches.
I made a slight change by using the following:
m = re.match('(.*)\[.*(?P<precost>\d+(\.\d*){0,1}).*\].*\[.*(?P<postcost>\d+(\.\d*){0,1}).*\]', '1) My Favorite Pokemon Mew *1 MP* [Pre-Avatar Mode Cost: 0.5 MP] [Post-Avatar Mode Cost: 0.5 MP]')
Though it appears that precost and postcost are both equal to “5”. Any idea what the issue may be with the regular expression?
I think a regex is the best bet for this:
That should work regardless of the number (or lack) of decimal places.