Skip to content

Instantly share code, notes, and snippets.

@ttrulock1
Last active January 4, 2022 02:09
Show Gist options
  • Save ttrulock1/cebaadd8a9fd64b3e51e96db7902f9d6 to your computer and use it in GitHub Desktop.
Save ttrulock1/cebaadd8a9fd64b3e51e96db7902f9d6 to your computer and use it in GitHub Desktop.
a tutorial explaining a specific regex
# REGEX TUTORIAL
## REGEX SUMMARY
REGEX is at its root, string pattern matching.
To extropolate, imagine having somebody to type in their phone number. There are number of different ways that this could be expressed varying from person to person to country to country, such as using parenthesis, etc. But if you want to store these numbers, they need to consistantly follow a similar pattern. REGEX is about following a pattern, and if you have a phone number, REGEX could test whether it was a valid phone number or not. Validation is a common use of regular expressions.
Regular expression allows you to look at strings and only a string. And regular expressions also allow you to setup a pattern then test that pattern against a string input.
Commonly REGEX is used for validating user input and data mining.
Using a Regex Email Address Example, I will explain some common Regex Syntex.
## REGEX EMAIL ADDRESS EXAMPLE
`/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/`
## REGEX SYNTAX EXPLAINED THROUGH REGEX EMAIL EXAMPLE
`^` - beginning of a string. In the email addresss example, `/^([a-z0-9_\.-]+)`, `^` makes sure the string begins following these patterns. Ex. toddstrulock would work in this context.
`$`- end of the string. In the case of the domain extension, `([a-z\.]{2,6})$`, the `$` used here expresses that this is the way the domain name and extension must end. EX. gmail.com
`()`- a grouping syntex. This is also know as a capture group. In the case here, `^([a-z0-9_\.-]+)`, the string could start with any letter a through z, number 0-9, -, or . or _. These () have to have an @ symbol at the end. EX. toddstrulock
`[]+` - `+` means how many character will match the pattern, and at least once and with no maximum any time the the `+` is used. In the case here, `[a-z\.]+` at least one of these requirements will be met. Example: [email protected] matches the `[a-z]` requirement.
`\.` - in regular expression the . is a wild card character meaning it could be anything; any character at all. If a literal . is needed then a `\.` is needed. In the example. of `([\da-z\.-]+)\.([a-z\.]{2,6})`, the `\.` represent the actual . in gmail.com.
`@`- In the case here, `/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/`, the @ MUST BE INCLUDED. It is a literal @ symbol and any string not containing the @ symbol will not be valid. Ex of a valid email [email protected] as compared to invalid email address toddstrulockgmail.com. `@` isn't exactly a REGEX syntex, but it demonstrates how REGEX syntex works outside of `[]`,`{}`, and `()`
`\d`- digit (meaning any number) can be used in the string. the \d in the example, `([\da-z\.-]+)\`, means any number, 0-9, would suffice within the the string. Ex. hotmail123.com
`{}` - the numbers within `{}` represents the number of characters within the string. In the email address example, `{2,6}` means the domain extension, i.e. .com, org,.gov, etc. This is not longer the six characters or smaller than two characters. Today, there a extensions longer than six characters.
## A Notible Ommision of common REGEX CODE in the Email example
`[^abc]` means not a, b, or c. If a carrot is inside of a square bracket such as `[^abc]` this means that a, b, or c would not work within the code. for example toddstrulock would work, but cameronbailey would not work in the example of `[^abc]`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment