<p>The following regex should work for validating that a string starts with one of <code>xpub</code>, <code>ypub</code>, <code>Ypub</code>, <code>zpub</code>, <code>Zpub</code>, <code>tpub</code>, <code>upub</code>, <code>Upub</code>, <code>vpub</code> or <code>Vpub</code> and is followed by 79-108 characters that are one of <code>123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz</code> (the Base58 alphabet) which is as close as you can get to validating BIP32 keys with regex:</p>
<pre><code>^([xyYzZtuUvV]pub[1-9A-HJ-NP-Za-km-z]{79,108})$
</code></pre>
<p>(try at: <a href="https://regexr.com/6cn9a" rel="nofollow noreferrer">https://regexr.com/6cn9a</a>)</p>
<p>If you want to accept BIP32 keys that optionally include the <code>?c={timestamp}&amp;h={bip}</code> arguments, then add <code>(?:(?:$|\?)((?!.*&amp;c=)c=\d{10}|(?!.*&amp;h=)h=bip\d{2,3})(?:(?=(?:&amp;c=|&amp;h=))&amp;(c=\d{10}|h=bip\d{2,3}))?)?</code> at the end, like this:</p>
<pre><code>^([xyYzZtuUvV]pub[1-9A-HJ-NP-Za-km-z]{79,108})(?:(?:$|\?)((?!.*&amp;c=)c=\d{10}|(?!.*&amp;h=)h=bip\d{2,3})(?:(?=(?:&amp;c=|&amp;h=))&amp;(c=\d{10}|h=bip\d{2,3}))?)?$
</code></pre>
<p>(try at: <a href="https://regexr.com/6cn9s" rel="nofollow noreferrer">https://regexr.com/6cn9s</a> - this will check that <code>c</code> is a valid 10-digit UNIX timestamp, <code>h</code> is <code>bip{2-3 digit number}</code>, that there is at most one of each parameter, and allow parameters to be in either order.)</p>
<p>However, a more accurate way to test that a BIP32 key is valid is to simply decode it with Base58Check (similar to how regular Bitcoin addresses are encoded), verify that the checksum is valid, and check that the length of the decoded data is exactly 78 bytes.</p>

CC BY-SA 4.0
2022-01-04T03:02:53.077Z
You could factor `pub` out of the brackets btw :)
meshcollider
CC BY-SA 4.0
2022-01-04T02:20:11.490Z
2022-01-04T22:39:12.100Z
2022-01-04T22:39:12.100Z
ieatpizza
<p>I am wondering if someone can help me with regex for validating all types xpubs like:</p>
<ul>
<li>xpub (mainnet P2PKH or P2SH)</li>
<li>ypub (mainnet P2WPKH in P2SH)</li>
<li>Ypub (mainnet P2WSH in P2SH)</li>
<li>zpub (mainnet P2WPKH)</li>
<li>Zpub (mainnet P2WSH)</li>
<li>tpub (testnet P2PKH or P2SH)</li>
<li>upub (testnet P2WPKH in P2SH)</li>
<li>Upub (testnet P2WSH in P2SH)</li>
<li>vpub (testnet P2WPKH)</li>
<li>Vpub (testnet P2WSH)</li>
</ul>
<p>Thank you.</p>

<address><public-key><xpub>
Regex for validating xpubs
Dan
# Regex for validating xpubs

(Asked by: username_0 on 2022-01-03)

I am wondering if someone can help me with regex for validating all types xpubs like:

* xpub (mainnet P2PKH or P2SH)
* ypub (mainnet P2WPKH in P2SH)
* Ypub (mainnet P2WSH in P2SH)
* zpub (mainnet P2WPKH)
* Zpub (mainnet P2WSH)
* tpub (testnet P2PKH or P2SH)
* upub (testnet P2WPKH in P2SH)
* Upub (testnet P2WSH in P2SH)
* vpub (testnet P2WPKH)
* Vpub (testnet P2WSH)

Thank you.

**Tags:** address, public-key, xpub


---

## 1 answers

### Answer by username_1 on 2022-01-04. Score: 2 | Accepted answer

The following regex should work for validating that a string starts with one of `xpub`, `ypub`, `Ypub`, `zpub`, `Zpub`, `tpub`, `upub`, `Upub`, `vpub` or `Vpub` and is followed by 79-108 characters that are one of `123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz` (the Base58 alphabet) which is as close as you can get to validating BIP32 keys with regex:

```
^([xyYzZtuUvV]pub[1-9A-HJ-NP-Za-km-z]{79,108})$

```

(try at: <https://regexr.com/6cn9a>)

If you want to accept BIP32 keys that optionally include the `?c={timestamp}&h={bip}` arguments, then add `(?:(?:$|\?)((?!.*&c=)c=\d{10}|(?!.*&h=)h=bip\d{2,3})(?:(?=(?:&c=|&h=))&(c=\d{10}|h=bip\d{2,3}))?)?` at the end, like this:

```
^([xyYzZtuUvV]pub[1-9A-HJ-NP-Za-km-z]{79,108})(?:(?:$|\?)((?!.*&c=)c=\d{10}|(?!.*&h=)h=bip\d{2,3})(?:(?=(?:&c=|&h=))&(c=\d{10}|h=bip\d{2,3}))?)?$

```

(try at: <https://regexr.com/6cn9s> - this will check that `c` is a valid 10-digit UNIX timestamp, `h` is `bip{2-3 digit number}`, that there is at most one of each parameter, and allow parameters to be in either order.)

However, a more accurate way to test that a BIP32 key is valid is to simply decode it with Base58Check (similar to how regular Bitcoin addresses are encoded), verify that the checksum is valid, and check that the length of the decoded data is exactly 78 bytes.

> **Comments:**
> **username_2**: You could factor `pub` out of the brackets btw :)  

---

