046 Regex

06 Jul 2013

Regular expressions or regex is a symbolic language to describe a pattern of characters so that we can match it with a required set. We can use regex pattern amtching across various programming languages to detect and match patterns as required.

Download video: mp4

Similar episodes: 008 Sublime Text

Background on Regex

  1. Regex Wikipedia
  2. Regex info
  3. Rubular - Ruby Regex
  4. RegexPal - JavaScript Regex
  5. Regexper - Create Regex

Things to learn with Regex

  1. open Rubular
  2. match any character with data Sherlock Holmes or hello Benedict Cumberbatch
  1. match neither character with data hello Benedict Cumberbatch
  1. match from the start of line with data http://build-podcast.com with escape sequence

    ^http:\/\/
    
  2. match from start till end with data 5th Symphony

  1. optional match with data me.com/blog or me.com/blog/3
  1. repetition
  1. alternative

Uses

  1. Text Editor: Sublime Text

    <h1>hello</h1>
    
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsam, ipsa amet facilis molestiae asperiores ex eum odio quod similique numquam!</p>
    

    with Find and Replace to detect all opening and closing tags

    <\/?[0-9a-z]*>
    
  2. Programming Languages: JavaScript

    1. Use RegexPal

      input

      <h1>hello</h1>
      

      regex

      <\/?[0-9a-z]*>
      
    2. Open up the Chrome DevTools - output as an array of matches

      var regex = /<;
      '<h1>hello</h1>'.match(regex)
      
  3. Finite-State Automaton : Using RegexPer to draw out a flow diagram that will deconstruct any regex. Using a pattern from 8 Regex you should know

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

More Resources on Regex

  1. Learn to code the hard way: Regex by Zed Shaw
  2. Tutsplus Course on Regex
  3. Learning Regex
  4. Regex reference
  5. Parsing HTML the Cthulhu Way and a reply in Stackoverflow

Build Link of this episode

Bret Victor's video channel on Vimeo