Git Product home page Git Product logo

phrg-regex-what-is-a-pattern's Introduction

RegEx - What Is A Pattern?

Objectives

  • Understand the purpose of patterns and regular expressions ("RegEx").
  • Learn a bit about the history of RegEx.
  • Understand that the version of RegEx we use is but one of many implementations.

Introduction

Say you're working at your new job as a developer and your supervisor asks you to build in validation for the email field in the company's signup form. There have recently been a lot of sign-ups with invalid email addresses (e.g., "joeflatiron.com", "@helloworld.com", and "$%![email protected]"). First, you sit down and come up with a set of rules that any email address should adhere to (stop reading and see how many you can come up with):

  • Numbers, letters, dashes, and underscores are ok.
  • Uppercase and lowercase letters are ok.
  • %, ?, $, !, * are not valid characters.
  • There must be an @ separating the local part from the domain part.
  • There must be at least one period in the domain part (e.g., gmail.com).
  • Two periods in a row (..) are not allowed.
  • The local (first) part of the email cannot start with a period.

We now have a pattern that we know all email addresses must follow. We use regular expressions to encode these patterns for matching, searching, and substitution. Here's a sample RegEx for email validation:

/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i

If this doesn't make any sense, don't worry. We'll be covering how to write and read regular expressions shortly.

(There are actually a LOT more rules for email addresses, but you get the point.)

About RegEx

History

RegEx came about in the 1950's and 1960's in various forms. Among the first appearances of regular expressions in program form was when Ken Thompson built Stephen Cole Kleene's notation into the editor QED as a means to match patterns in text files. Since then, there have been various implementations of regular expressions developed. We'll be using Ruby regular expressions, an implementation mostly based off the PERL language.

When to use RegEx

Regular expressions are an extremely powerful way to search through strings and blocks of text for specific patterns. They can be used for data validation, searching, mass file renaming, and finding records in a database. Use them carefully. They are like a surgeon's scalpel: able to do a lot of harm or good, depending on how skillfully they are wielded.

PHRG RegEx - What Is A Pattern?

Does this need an update?

Please open a GitHub issue or pull-request. Provide a detailed description that explains the issue you have found or the change you are proposing. Then "@" mention your instructor on the issue or pull-request, and send them a link via Connect.

phrg-regex-what-is-a-pattern's People

Contributors

annjohn avatar aviflombaum avatar dfenjves avatar franknowinski avatar ga-be avatar garettarrowood avatar louiefigz avatar web-kat avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

web-kat

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.