Skip to content

Instantly share code, notes, and snippets.

@jgarcia45
Last active June 14, 2022 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgarcia45/9c5bbcf8362cef4c4bed55216041d14a to your computer and use it in GitHub Desktop.
Save jgarcia45/9c5bbcf8362cef4c4bed55216041d14a to your computer and use it in GitHub Desktop.
Gives a description and example of what Regular Expression (Regex) is and does.

Regular Expression (Regex)

The purppose of this tutorial is to give users information on what Regex is and how to use it. The tutorial will provide a summary as well as detail examples for a specific regex provide.

Summary

Regular Expression, otherwise known as "Regex"; is a sequence of characters that defines a specific search pattern. Regex can be used to find certain patterns of characters within a string. However, Regex is most frequently used to validate inputs from a user. An example is the following Regex provided below.

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

The regex is used to verify that the user input is a valid email address. More details of this Regular Expression will be provided below

Table of Contents

Regex Components

Anchors

Anchors allow the strings to match your input. Those anchors would be ^ and $. The ^ anchor matches the beginning of the string; while the $ anchor matches the end of the string. So for the example of the email validation, "/^([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6})$/", the anchor will match the exact string of the email. The reason is because both characters are at the beginning and end of the validation.

Quantifiers

Quantifiers are the characters: *, +, ?, and {}. Quantifiers match the specified quantity of the previous token. So an example with the email validation, "/^([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6})$/", the quantifier will match the tokens from 2 to 6 within {} and match the string with +.

OR Operator

OR Operator are the characters: | and []. OR Operator matches the characters within the tokens. So an example with the email validation is, "/^([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6})$/", The [] looks for those tokens and matches them specifically.

Grouping and Capturing

Grouping and Capturing are the characters: (). It groups multiple tokens together to create a capture group for extracting a substring. So with the email validation, "/^([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6})$/", it is capturing the group together within the parentheses.

Bracket Expressions

Bracket Expression uses the characters: []. Bracket Expressions matches the string between the characters. With the email validation, "/^([a-z0-9_\.-]+)@([\da-z\.-]+).([a-z\.]{2,6})$/", the Bracket Expression matches the characters within [] expresssion.

Greedy and Lazy Match

Greedy operators are the characters: * + {}. The lazy operator is the character: ?. Greedy operators match the token to the preceding token. The lazy operator makes the quantifier lazy which makes it match as few characters as possible. So in the email validation, "/^([a-z0-9_.-]+)@([\da-z.-]+).([a-z.]{2,6})$/", there is no lazy operator. However, it does contain greedy operators which allows the tokens to match specifically.

Author

The gist was created by Juan Garcia. Juan is currently attending a Coding Boot Camp at Michigan State University. He received his Bachelor's Degree in Computer Engineering with a Minor in Computer Science. You can check the links below to know more about Juan:

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