Skip to content

Instantly share code, notes, and snippets.

@mehdisafari77
Last active November 10, 2021 23:58
Show Gist options
  • Save mehdisafari77/303347ea7cf2de4d3fb04a793991db03 to your computer and use it in GitHub Desktop.
Save mehdisafari77/303347ea7cf2de4d3fb04a793991db03 to your computer and use it in GitHub Desktop.

Regex Tutorial

Table of Content

License: MIT
Regex
Example
Authors
Acknowledgments

What is Regex?

A regular expression or (REGEX) is a sequence of characters that specifies a search pattern. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation. It is a technique developed in theoretical computer science and formal language theory.

Example

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

  • What does this Regex mean step by step?
  • / Opening and closing any Regex with a forward slash / is a necessity.
  • ^ at the beginning and $ at the end means that the following inout is a string, which are called anchors. It starts with ^ and ends with $.
  • [a-z0-9_.-] This means that the email can contain letters from a-z, numbers from 0-9, _.- means that the email can contain an underscore, dash, any special character, period, and a dash.
    • sign indicates (one or more) of the characters a rule in the Regex.
  • @ This means that after the initial rule, and @ sign is needed.
  • [\da-z.-]+ means that the input can contain one or more digits, and letters from a-z, any special characters(.) and dashes.
  • [a-z.] means that the input can contain a letter from a-z, a speical character.
  • {2,6} means that the input shpuld be between 2-6 characters.

Authors

  • Mehdi Safari

Acknowledgments

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