Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: cloverme on November 25, 2015, 02:12:55 PM



Title: Regex for public keys (production and testnet)?
Post by: cloverme on November 25, 2015, 02:12:55 PM
I have a regex for matching public keys for the standard bitcoin public key, but it doesn't match testnet public key addresses.  Does anyone have one that matches both?

Public Key Match for prod:
/^[13n][1-9A-Za-z][^OIl]{20,40}/



Title: Re: Regex for public keys (production and testnet)?
Post by: DannyHamilton on November 25, 2015, 02:51:41 PM
I have a regex for matching public keys for the standard bitcoin public key, but it doesn't match testnet public key addresses.  Does anyone have one that matches both?

Public Key Match for prod:
/^[13n][1-9A-Za-z][^OIl]{20,40}/

There appears to be a bug in your regex.

When I test it against the following:
Code:
1O!@#$%^&*()-=_+[]{};':",./<>?`~

It claims that it is a match.

Perhaps consider something like the following to match mainnet addresses instead:

/^[13][1-9A-HJ-NP-Za-km-z]{26,35}/

If you want to find testnet addresses you could try:

/^[2mn][1-9A-HJ-NP-Za-km-z]{26,35}/

If you want to match both testnet AND bitcoin addresses, you could try:

/^[123mn][1-9A-HJ-NP-Za-km-z]{26,35}/

Note that these REGEX that I've supplied should match all the addresses indicated, but they will also match some strings that are NOT valid addresses.  If you really want to be certain that you are finding actual addresses, you'll need to verify the checksum.


Title: Re: Regex for public keys (production and testnet)?
Post by: Shibashi Dogemoto on November 25, 2015, 02:56:52 PM
I have a regex for matching public keys for the standard bitcoin public key, but it doesn't match testnet public key addresses.  Does anyone have one that matches both?

Public Key Match for prod:
/^[13n][1-9A-Za-z][^OIl]{20,40}/

There appears to be a bug in your regex.

When I test it against the following:
Code:
1A!@#$%^&*()-=_+[]{};':",./<>?`~

It claims that it is a match.

I'm not a regex guru, but if i read this correctly, the regex says:
Start with a 1,3 or n
Then a number between 1 and 9 or any letter (upper or lowercase)
Then between 20 and 40 characters, any character will do except OI and l...

So, basically, your test is supposed to match (starts with 1, then an A, then between 20 and 40 random characters (but no O,I or l)

If somebody points me to the correct defenition of a mainnet and testnet key, i can take a stab at writing a regex (no promises tough)


Title: Re: Regex for public keys (production and testnet)?
Post by: cloverme on November 26, 2015, 02:40:41 AM

There appears to be a bug in your regex.

When I test it against the following:
Code:
1O!@#$%^&*()-=_+[]{};':",./<>?`~

If you want to match both testnet AND bitcoin addresses, you could try:

/^[123mn][1-9A-HJ-NP-Za-km-z]{26,35}/

Note that these REGEX that I've supplied should match all the addresses indicated, but they will also match some strings that are NOT valid addresses.  If you really want to be certain that you are finding actual addresses, you'll need to verify the checksum.

This did the trick for me... Agreed on checksum too.  Although, I can rely on the validateaddress api call to bitcoind as well. Mainly, I wanted the regex to keep people from spamming a bitcoin address in chat.

Much appreciated for your help sir!