Skip to content

Instantly share code, notes, and snippets.

@campe0n
Last active July 24, 2021 18:52
Show Gist options
  • Select an option

  • Save campe0n/ba26234752260e32f03d6e9b15435ee8 to your computer and use it in GitHub Desktop.

Select an option

Save campe0n/ba26234752260e32f03d6e9b15435ee8 to your computer and use it in GitHub Desktop.
Regex Tutorial

Regex Tutorial

Regular Expression (Regex) utilizes a string of values and characters to search for a specific string, also called a match. Regex is useful for finding multiple matches within multiple lines of code. Some uses of regex include finding and replacing code and used for input validation. Keep in mind, that this is not a comprehensive regex guide, but attemps to explain regex through the expression below. There are other components that are not explained here. If you wish to learn more about regex this website is an excellent resource: https://regexr.com/61lrg.

Summary

Email Regex


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

This regular expression begins with the beginning anchor (/^). It then groups any characters from a-z and numbers from 0-9. In addition to these values, this group also includes an underscore, and escaped period, a dash and lastly a (+) quantifier. This group creates the first string of an email preceding the @ symbol. Next, the @ symbol is searched for. Then another group is created that contains any digit character denoted by (\d), any letters from a-z, another escaped period, dash and quantifier. This string is located between the @ symbol and a period in an email. Lastly, we group up the domain of an email. Ex: .com, .net, .fr. The regex will search for characters between a-z and an escaped period. This group differs from the previous two groups because it uses a quantifiers denoted by brackets and numbers. In our email regex, {2,6}, will search for only two to six characters attached to the [a-z.] bracket expression.

Table of Contents

Regex Components

Anchors


^abc$

The (^) symbol matches the beginning of a string. While the ($) matches the end of the string. These components are exclusively found at their respective locations on a string and are used to signify the start and end of a regex.

Quantifiers


a+, a{2,6}

The (+) quantifier matches 1 or more of the preceding characters. The ({2, 6}) quantifier matches 2 to 6 characters. Bracket qunatifiers search for indivual characters not strings.

Grouping Constructs


(abc)

The grouping constructs denoted by parenthesis () group characters together to be searched. In our email regular expression, we use grouping constructs to group the string before the @ symbol, the string before the . and lastly the domain. Ex: .com, .net.

Bracket Expressions


[a-z], [0-9]

Bracket expressions in regex are used to search for a quantity of characters specified between the brackets. In our email expression, we use [a-z0-p] and other characters to search for characters between a-z and numbers 0-9. In our expression, we combined the a-z and 0-9 ranges in one bracket pair.

Flags


/g

Regex uses flags to further specify a search. The /g flag stands for global expression and allows the expression to search for more than one match within lines of code. Flags are located at the end of the expression.

Character Escapes


\.

Regex uses the characters: period, star symbol, and backslash. If you wish to seach for one of these three characters you can do so by denoting it with a backslash. In our example, we need to use the period character escape, so our expression knows that we want to match a period. Instead of a periods normal function in regex.

Author

Melvin Finn. I am a student in a coding program. Github: https://github.com/campe0n

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