Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex does not work as expected in interpreter

I want to search through a string and find units in fahrenheit and convert them into celcius.

To do this, my approach is to user regex to find units in fahrenheit in a given string, and if I find any, get the number and convert it into celcius.

This is the regex I have come up with:

regexp = re.compile("\d+(.*?)(\bfahrenheit\b|\bf\b)", re.IGNORECASE)

And here is the regex in action.

On the Pythex site it seems to work OK, however, in my interpreter the same regex does not match anything.

Any suggestions?

like image 770
Michael Gradek Avatar asked Dec 02 '25 05:12

Michael Gradek


1 Answers

You need to use a raw string, or \b will mean "backspace" instead of "word boundary":

regexp = re.compile(r"\d+(.*?)(\bfahrenheit\b|\bf\b)", re.IGNORECASE)
like image 67
Tim Pietzcker Avatar answered Dec 03 '25 19:12

Tim Pietzcker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!