Author

Topic: Regex for public keys (production and testnet)? (Read 1350 times)

legendary
Activity: 1512
Merit: 1057
SpacePirate.io
November 25, 2015, 09:40:41 PM
#4

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!
hero member
Activity: 840
Merit: 1000
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)
legendary
Activity: 3472
Merit: 4801
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.
legendary
Activity: 1512
Merit: 1057
SpacePirate.io
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}/

Jump to: