Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Allan Pedersen
Allan Pedersen
2,421 Points

Matching ½ etc. with a float

How do I match i.e. ½ with a float using regex on a string? As an example I need to fetch some information around recipes and that contains i.e. 2 ½ oz of something. In this example I would like to turn 2 ½ into 2.5.

Any good ideas? I've tried Google my way out of it without luck.

1 Answer

Allan Pedersen
Allan Pedersen
2,421 Points

Correction. I found suggestions on Stackoverflow that suggest doing a .replace("½", ".5"). The question is perhaps then also if this is best practice? Perhaps there's a better way.