Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regexextract decimal number from text with decimals

I have a simple google sheets REGEXEXTRACT function that extracts numbers from NFL lines and over unders.

Here's the problem: The function

=VALUE(REGEXEXTRACT(I3,"-*\d*.?\d+"))

properly extracts -13.5 from text Line: GB -13.5 But when I use the same function on the text O/U: 51.5, it incorrectly extracts 51.0

Where is my regular expression failing me?

like image 915
ryanbuckner Avatar asked Oct 20 '25 14:10

ryanbuckner


1 Answers

The problem with your current regex is that, as written, in the second case the leading portion of the pattern is not matching anything, and only the final \d+ matches the integer 51 (see the demo here). You see 51.0 in your Excel spreadsheet because the decimal component defaults to being zero. Please use this regex pattern instead:

-?\d+(?:\.\d+)?

Demo

like image 190
Tim Biegeleisen Avatar answered Oct 22 '25 03:10

Tim Biegeleisen