Bitcoin address regex

A Bitcoin address, or simply address, is an identifier of 26-35 alphanumeric characters, beginning with the number 1 or 3, that represents a possible destination for a Bitcoin payment. Most Bitcoin addresses are 34 characters. Then lets check for digits, and lowercase and bitcoin address regex letters. But to prevent visual ambiguity we need to remove O, I, l and 0.

We then need to make sure it start with either 1 or 3. Note how the range had to be shifted to take the first character into consideration. Lastly we need to make sure that the text to pass contains only the Bitcoin address, so not stuff like: somethingsomething else. Hello my name is Giovanni, but on the web I’m mokagio.

I’m a freelance iOS engineer and I’m available for contract work. This place is a collector of random findings regarding little details of the software development craft. You should check out my other blog: mokacoding, where I write proper posts, rather than random notes. M9 1a8 8 0 1 0 0 16A8 8 0 0 0 9 1zm. Join Stack Overflow to learn, share knowledge, and build your career. M9 1a8 8 0 1 0 0 16A8 8 0 0 0 9 1zM8 15.

Thing is, I’m not good with regular expressions and I haven’t found a single source to confirm this would not create false negatives. Your referenced page has a section “Address validation”. Why not use the technique provided in the link over there? If all bitcoin addresses have that format, then I don’t see a reason why this wouldn’t work. You can’t validate a bitcoin address using a regexp because all bitcoin addresses have that checksum. It is true that you regexp will discard many addresses which are clearly invalid But your regexp will also accept an insane number of invalid ones. This is not for an application open to the public, only to a couple developers.

The whole point is that if they have a typo or copy only half of the address, for the app to warn them. However, as mentioned in a comment, a regex is not a good way to validate a bitcoin address. It seems to me the purpose of the regex is finding potential bitcoin addresses, not necessarily valid ones. Thanks for providing at least partially correct solution rather than whining about what can’t be done like rest of posters.