Skip to content

Instantly share code, notes, and snippets.

@Xavionstar
Last active March 26, 2023 07:13
Show Gist options
  • Save Xavionstar/2ad9ce9a15acf2f49c101d0772b324bf to your computer and use it in GitHub Desktop.
Save Xavionstar/2ad9ce9a15acf2f49c101d0772b324bf to your computer and use it in GitHub Desktop.
Regex Reduced to Relatable Rhetoric

Regex Tutorial

This tutorial will explain the use of regex, how to implement it, and the proper syntaxes.

Summary

This is an example of regex that will call out anything matching an email format. Anything with the "[email protected]" format.

/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/

Table of Contents

Regex Components

Regex has multiple components and must be wrapped in a the (/) forward slash. We will go into detail with examples below.

Anchors

In Regex an anchor is a symbol that indicates a specific position within a string. The front and back symbols are the caret ^ and the dollar-sign $. The ^ is used to match the beginning of a string, so if you use the regex "^sing", it will match anything that begins with the word "sing", like "singed", but not "phasing". The $ is used to match the end of a string, so if you use the regex "ship$", it will match any string that ends with the word "ship", like "relationship", but not "shipyard".

/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/

In my matching email regex you can see the ^ at the beginning /^( and the $ at the end )$/ indicating the start and finish of what you are searching for.

Quantifiers

Imagine you're trying to find a group of dogs in a big field. You don't know how many dogs are in the group, but you know they're all together. Quantifiers in regex are going to help you count the dogs. They tell you how many times a certain letter or group of letters appears. The asterisk \* means zero or more times. It means you can find the pattern whether there are no dogs or a bunch of dogs in the group. The plus sign + means at least one or more times. It means you can find the pattern only if there is at least one dog in the group. The main difference here is asterisk allows for zero and plus does not.

/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/

In my matching email regex you can only see the + which indicates it MUST have the characters preceding it. It cant be zero matching characters because you cant have an email that begins with @.

Grouping Constructs

Lets say you are looking for your friends in a bar. You know a bunch of your friends are wearing the same color shirt. A Grouping Construct would help you find your friends by their matching shirts. You could use the parentheses to find green shirts by looking for (green shirts). This would tell regex to look for all groupings of specifically green shirts so that you dont find "green drinks" or "red shirts".

/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/

In my matching email regex it uses parentheses around each part of the email to identify it as a unit: The "something" @ "somewhere" . "com".

Bracket Expressions

Bracket Expressions let you search for specific types of something in a group. As an example, say you have a big basket of fruit. A bracket expression would let you search for specific types of fruit in the basket. You could input [a] and it will look for any apples in the basket. You could also use it to look for a range of letters. If you input [a-c] it would look for all the apples, bananas and canteloupes. If you use the ^ before the letter or range it will look for everything besides it. So [^a-c] would look for everything EXCEPT the apples, bananas and canteloupes.

/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/

In my matching email regex it uses brackets to tell the quantifier what its looking for: [a-z0-9_\.-]+ is telling it to look for something a-z and 0-9 and it allows hyphens, underscores, and periods.

Character Classes

Character classes are predefined regex expressions that match common groupings. Some examples are \d which looks for any digit characters like 0-9 or \w which looks for any word characters. In the case of character classes, capitalizing the letter searches for the opposite. So \D looks for any NON digit character.

/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/

In my matching email regex it uses the [\da-z\ to indicate the digits as well as a-z.

Character Escapes

In regex you use character escapes anytime you would use a character that has a special meaning in regex but you want to use it in its normal use. For example, in my matching email regex, it uses this /^([a-z0-9_\.-]+) to show that in addition to a-z and 0-9 it is also looking for periods in the email name. Or \.([a-z\.] to show the .com.

Author

My name is Lincoln and im a student at the UW coding bootcamp. In the course of learning front and back end software developement I can now add regex to my toolbelt. Lincoln Winter Pough [email protected] https://github.com/Xavionstar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment