Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: chetrasho on May 22, 2011, 05:32:55 PM



Title: Regex for bitcoin amounts?
Post by: chetrasho on May 22, 2011, 05:32:55 PM

Hi,

I'm writing a PHP script for bitcoins. I need a regex to check if a string is a valid number of bitcoins.

I've been trying stuff like [0-9]{0,8}\.[0-9]{0,8} but I know that's not really right. I suck at regexes...

Any help would be greatly appreciated...

Thanks
TRSH0


Title: Re: Regex for bitcoin amounts?
Post by: chetrasho on May 22, 2011, 05:35:44 PM

I found this thread: http://forum.bitcoin.org/index.php?topic=2459.msg33802#msg33802

But I'm still sorta lost on how to implement stuff in PHP. I just wanna do preg( $bitcoinregex, $bitcoinamount)...


Title: Re: Regex for bitcoin amounts?
Post by: kseistrup on May 22, 2011, 05:38:32 PM
Would it be easier to convert the amount to a float, then assert that the number is positive and <= 21E6?

Cheers,


Title: Re: Regex for bitcoin amounts?
Post by: chetrasho on May 22, 2011, 05:54:26 PM

Well, the variable $bitcoinamount is coming form post data $_POST['bitcoinamount']. So it's not necessarily a number to begin with.

I guess I could do something like

if ( is_float($ba) &&  $ba <= 21E6 && num_decimals($ba) <= 8 ) { return true; }

But I need a num_decimals() function.


Title: Re: Regex for bitcoin amounts?
Post by: kseistrup on May 22, 2011, 06:00:47 PM
I don't speak PHP, but in Python you could do something like:

Code:
def num_decimals(amount_string):
    (integer, decimals) = amount_string.split('.')
    return len(decimals)

Cheers,


Title: Re: Regex for bitcoin amounts?
Post by: bittrader on May 22, 2011, 06:29:20 PM
Something like this would probably work.

Code:
$btc = $_POST['bitcoinamount'];

// Verify that the amount is (1) less than 21 million, (2) contains only numerals and an optional decimal point, and (3) contains a maximum of eight number after the decimal point.
if (preg_match( '/^[0-9]*\.?[0-9]{0,8}$/', $btc) && $btc <= 21000000)
  echo "valid";
else
  echo "fail";

Depending what you're doing, you might want to also verify that the amount isn't zero.


Title: Re: Regex for bitcoin amounts?
Post by: chetrasho on May 22, 2011, 06:40:11 PM

Awesome. That's basically what I need, bittrader. Thank you so much!


Title: Re: Regex for bitcoin amounts?
Post by: theymos on May 22, 2011, 11:53:27 PM
I think this would work as a pure-regex solution:
Code:
^0*(1?[0-9]|20)?[0-9]{0,6}(\.[0-9]{0,8}0*)?$


Title: Re: Regex for bitcoin amounts?
Post by: chetrasho on May 23, 2011, 01:51:10 AM


Nice. Thank you. Regexs are pretty sweet. It's just a few characters but over time I hope your work helps tons of developers...


Title: Re: Regex for bitcoin amounts?
Post by: nopara73 on April 01, 2016, 06:10:46 PM
I think this would work as a pure-regex solution:
Code:
^0*(1?[0-9]|20)?[0-9]{0,6}(\.[0-9]{0,8}0*)?$

This is a good starting point, but it is not perfect. (I am not going to give the solution, but I want to note it before someone starts to use it blindly based on a quick google search.) 
For example it matches the input: 0000000000000000000000000000000009999999.00000000000000000000000000000000000000 0000000000000000
Also it doesn't let the "," to be used instead of ".", in some case you might want that that.