Last active
January 23, 2022 17:21
-
-
Save cluck135/08fa52ed227010daafb0d04c5617cb1d to your computer and use it in GitHub Desktop.
Regex Tutorial for Checking Bitcoin Addresses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Regex Tutorial for Checking Bitcoin Addresses | |
A simple regex tutorial explaining how the characters in regex code determine what text entries will fit the Regex Code. | |
## Summary | |
Regex is great for checking that user inputs are correct, such as emails, usernames, and passwords. In this tutorial I will be teaching you how | |
Regex works for checking that Bitcoin addresses fit the criteria necessary to be a valid address. Although it won't check if the address is | |
in fact owned by someone. Just that it fits the criteria to be an Bitcoin address that someone could send BTC too. | |
``` | |
^(?:[13]{1}[a-km-zA-HJ-NP-Z1-9]{26,33}|bc1[a-z0-9]{39,59})$ | |
``` | |
## Table of Contents | |
- [Anchors](#anchors) | |
- [Quantifiers](#quantifiers) | |
- [Grouping Constructs](#grouping-constructs) | |
- [Bracket Expressions](#bracket-expressions) | |
- [The OR Operator](#the-or-operator) | |
## Regex Components | |
### Anchors | |
Using ^ signify that the string we are checking must start with the character right after ^ and not have anything before it. | |
While the $ signifies that the string must end with the preceding characters before $ and nothing after it besides a single new line. | |
### Quantifiers | |
The quanitifiers include {26,33} where it specifies that the previous brackets must be between 26 and 33 characters long. No more or less. | |
The second quantifier is {39,59} where the previous bracket must be between 39 and 59 characters. | |
### Grouping Constructs | |
?: is used as a non-grouping construct to [13]. meaning that the construct isn't saved for later to be used via a nunmbered reference. | |
if just : was used then it would be a capturing construct. | |
### Bracket Expressions | |
``` | |
[13] | |
``` | |
means its looking for either a 1 or 3 in that spot in the text at that spot. | |
``` | |
[a-km-zA-HJ-NP-Z1-9] | |
``` | |
is looking for lower case letters from a-k. Then looking for lower case letters from m-z. Then looking for Upper | |
Case letters from A-H. Then for upper case letters from J-N. Then for uppercase letters from P-Z. Then numbers from 1-9 not including 0. | |
Its important to note that these are not in any order, so numbers can come first even though the brackets start with a-k. | |
``` | |
[a-z0-9] | |
``` | |
same concept applies here for this bracket. This one in particular includes all letters and numbers, but no upper case letters. | |
``` | |
bc1 | |
``` | |
the bc1 following the | operator means the regex is looking for a string that starts with bc1 in that order and that case. | |
### The OR Operator | |
``` | |
{26,33}|bc1 | |
``` | |
Here we have a | which means that anything before the | or after the | will fit to the regex code. So the | |
``` | |
?:[13]{1}[a-km-zA-HJ-NP-Z1-9]{26,33} | |
``` | |
alone will look for a string that fits that particular regex and the | |
``` | |
bc1[a-z0-9]{39,59} | |
``` | |
will look for a string that fits that regex code. The | is essentially the same as and Or statement in coding. | |
if the code fits either criteria it will pass the regex code. | |
## Author | |
Casen Luck, I am a full stack developer who works on various coding projects with an interest in crypto and blockchain tech | |
My Github Profile: [cluck135](https://github.com/cluck135) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
finished explanation of all the regex code