Regex for Dogecoin Addresses

Which regular expression can be used to match a Dogecoin address?

An expression that matches a Dogecoin Address must ensure the address starts with a D, followed by one character from the digits 5 to 9 or capital letters A to U (but excluding capital letters I and O), and finally followed by 32 more random alphanumeric characters but excluding the digit 0, lowercase letter l, uppercase letter I, and uppercase letter O.

A regular expression that does all of this is:

/^D[5-9A-HJ-NP-U]{1}[1-9a-km-zA-HJ-NP-Z]{32}$/Edit with Regexity

Let’s break this down and see how it all works.

General Format of a Dogecoin Address

Dogecoin addresses are generally formatted in the following way:

The exclusions in the final bullet above are also present in Bitcoin addresses and are meant to prevent confusion when reading the address. Depending on the font, someone might confuse the digit 0Edit with Regexity for the uppercase letter OEdit with Regexity, or confuse the lowercase letter lEdit with Regexity with the uppercase letter IEdit with Regexity, and end up typing a totally different address than was initially intended. Hence the exclusions.

Regular Expression for Dogecoin Address

Let’s build an expression to satisfy the criteria above. Firstly, the expression should start with a capital letter DEdit with Regexity:

/D/Edit with Regexity

This must be followed by 1 character from the set 5Edit with Regexity to 9Edit with Regexity or capital letters AEdit with Regexity to UEdit with Regexity (but excluding capital letters IEdit with Regexity and OEdit with Regexity). To do this, we specify the range of possible characters in a character set using square brackets []Edit with Regexity. Note that we construct the character ranges in such as way that it “skips over” the capital letters IEdit with Regexity and OEdit with Regexity:

/D[5-9A-HJ-NP-U]{1}/Edit with Regexity

The {1}Edit with Regexity above is a quantifier that specifies that we will allow exactly 1 character from the square brackets.

Now we can specify the 32 alphanumeric characters in square brackets. The exclusions mentioned above are incorporated by specifying various character ranges that skip over the excluded characters.

/D[5-9A-HJ-NP-U]{1}[1-9a-km-zA-HJ-NP-Z]{32}/Edit with Regexity

As before, the {32}Edit with Regexity specifies that we will allow exactly 32 characters (no more and no less) from the square brackets.

And finally, we add the start-of-string (^Edit with Regexity) and end-of-string ($Edit with Regexity) anchors to ensure that the string we are validating contains only one valid Dogecoin address and nothing else before or after it.

/^D[5-9A-HJ-NP-U]{1}[1-9a-km-zA-HJ-NP-Z]{32}$/Edit with Regexity

Which Flags to Use With This Expression

If you’re simply trying to validate a Dogecoin address, there’s no need to use any flags. In particular, do not use the i (case insensitive) flag, since Dogecoin addresses are indeed case sensitive, as we’ve seen with the exclusions mentioned above.

If you’re trying to extract all Dogecoin addresses from a given string of text, you’ll need to turn on the g (global) flag, which tells the regex pattern to retrieve all matches, and not just the first one. In this case, you should also remove the start-of-string (^Edit with Regexity) and end-of-string ($Edit with Regexity) characters:

/D[5-9A-HJ-NP-U]{1}[1-9a-km-zA-HJ-NP-Z]{32}/gEdit with Regexity

Sources

The regular expressions on this page were adapted from expressions found on this Reddit thread and this Node module on Github. In addition, this Reddit thread was helpful in determining acceptable characters for the second character following the D.

Benjamin

Founder, owner, and sole content creator on RegexLand. Enjoys programming, blogging, and teaching others how to do the same. Read more...

Leave a Comment