$30 off During Our Annual Pro Sale. View Details »

Demystifying the Wizardry of Regular Expressions - LV DotNet Meetup - 01/2016

kickinbahk
January 28, 2016

Demystifying the Wizardry of Regular Expressions - LV DotNet Meetup - 01/2016

So many developers see the words "Regular Expressions" and think of a frightening string of crazy symbols. While this is true, they are also a valuable way of manipulating text and knowing the basics can lead to many practical uses that will often save many lines of code. Learning to use regular expression basics will allow many more ways to manipulate text and strings by testing, extracting, and changing the string. We will take a look at some of the ways to do this specifically using the Javascript RegExp Engine.

kickinbahk

January 28, 2016
Tweet

More Decks by kickinbahk

Other Decks in Technology

Transcript

  1. Regular Expression Wizardry
    Josiah Mory @kickinbahk

    View Slide

  2. Let’s set the stage…

    View Slide

  3. /^[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+$/i

    View Slide

  4. View Slide

  5. /^[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+$/i

    View Slide

  6. View Slide

  7. Maybe someone was having
    a bad day

    View Slide

  8. What is a Regular
    Expression?

    View Slide

  9. Regular Expression
    =
    a pattern describing a
    specific string of text

    View Slide

  10. /regex-pattern-here/

    View Slide

  11. /[a-z]/

    View Slide

  12. Regular Expression "Engine"
    =
    a piece of software which
    processes regular expressions
    and tries to match the pattern
    to the given string

    View Slide

  13. The syntax and
    behavior of a particular
    engine
    =
    regular expression flavor

    View Slide

  14. 3 Things You Do with
    Regular Expressions

    View Slide

  15. Search
    a string to see if it matches your
    pattern

    View Slide

  16. Extract
    a string (or part of a string) that matches
    your pattern

    View Slide

  17. Replace
    a string by replacing parts that match
    with other text

    View Slide

  18. “Find-and-Replace on Steroids”
    - Dan Nguyen

    View Slide

  19. Literal Characters

    View Slide

  20. Most Characters:
    • a - z
    • A - Z
    • 0 - 9

    View Slide

  21. Special Characters

    View Slide

  22. \, ^, $, ., |, ?, *, +, (, ), [, [,
    12 special or (meta) characters

    View Slide

  23. * If you want to use any of these
    characters as a literal in a regex, you
    need to escape them with a backslash

    View Slide

  24. View Slide

  25. View Slide

  26. View Slide

  27. gandalf_quote1 = "You shall not pass! -Gandalf"
    ~> You shall not pass! -Gandalf The Grey
    the_grey = “ The Grey”
    console.log(gandalf_quote + the_grey);

    View Slide

  28. gandalf_quote2 = "Yes, yes my dear sir and I do know your name Mr.
    Bilbo Baggins. And you do know my name, though you don't
    remember that I belong to it. I am Gandalf ” + the_grey + “ and
    Gandalf ” + the_grey + “ means me."
    gandalf_quote2 = "Yes, yes my dear sir and I do know your name Mr.
    Bilbo Baggins. And you do know my name, though you don't
    remember that I belong to it. I am Gandalf, and Gandalf, means me."

    View Slide

  29. View Slide

  30. Lookaheads
    ✤ Negative - ?!
    ✤ Positive - ?=

    View Slide

  31. Asserts that what
    immediately follows the
    current position in the string
    is not foo
    Negative Lookahead
    (?!foo)

    View Slide

  32. Asserts that what
    immediately follows the
    current position in the string
    is foo
    Positive Lookahead
    (?=foo)

    View Slide

  33. Capture Groups
    () allow us to group a RegEx
    together

    View Slide

  34. Groups are numbered 1-99

    View Slide

  35. You can call a Group using $ and
    the number
    E.g. $1

    View Slide

  36. To not capture a group in parens
    ?:

    View Slide

  37. /.*\.com\/(?:(?:groups\/videos\/)|
    (?:ondemand|channels)(?:(?:\/less\/)|(?:\/
    \w*\/))|(?:video\/))?([0-9]+).*/

    View Slide

  38. /.*\.com\/
    (?:(?:groups\/videos\/)|(?:ondemand|
    channels)(?:(?:\/less\/)|(?:\/\w*\/))|(?:video
    \/))?([0-9]+).*/

    View Slide

  39. /.*\.com\/
    (?:(?:groups\/videos\/)
    |(?:ondemand|channels)(?:(?:\/less\/)|(?:\/
    \w*\/))|(?:video\/))?([0-9]+).*/

    View Slide

  40. /.*\.com\/
    (?:(?:groups\/videos\/)
    |(?:ondemand|channels)(?:(?:\/less\/)
    |(?:\/\w*\/))|(?:video\/))?([0-9]+).*/

    View Slide

  41. /.*\.com\/
    (?:(?:groups\/videos\/)
    |(?:ondemand|channels)(?:(?:\/less\/)
    |(?:\/\w*\/))
    |(?:video\/))?([0-9]+).*/

    View Slide

  42. /.*\.com\/
    (?:(?:groups\/videos\/)
    |(?:ondemand|channels)(?:(?:\/less\/)
    |(?:\/\w*\/))
    |(?:video\/))?([0-9]+).*/

    View Slide

  43. /.*\.com\/
    (?:(?:groups\/videos\/)
    |(?:ondemand|channels)(?:(?:\/less\/)
    |(?:\/\w*\/))
    |(?:video\/))?([0-9]+).*/

    View Slide

  44. /.*\.com\/
    (?:(?:groups\/videos\/)
    |(?:ondemand|channels)(?:(?:\/less\/)
    |(?:\/\w*\/))
    |(?:video\/))?([0-9]+).*/

    View Slide

  45. /.*\.com\/
    (?:(?:groups\/videos\/)
    |(?:ondemand|channels)(?:(?:\/less\/)
    |(?:\/\w*\/))
    |(?:video\/))?([0-9]+).*/

    View Slide

  46. /.*vimeo.com\/
    (?:(?:groups\/videos\/)
    |(?:ondemand|channels)(?:(?:\/less\/)
    |(?:\/\w*\/))
    ?(?:video\/))?(?:[0-9]+)?.*/

    View Slide

  47. /.*\.com\/
    (?:(?:groups\/videos\/)
    |(?:ondemand|channels)(?:(?:\/less\/)
    |(?:\/\w*\/))
    |(?:video\/))?([0-9]+).*/
    /.*vimeo.com\/
    (?:(?:groups\/videos\/)
    |(?:ondemand|channels)(?:(?:\/less\/)
    |(?:\/\w*\/))
    ?(?:video\/))?(?:[0-9]+)?.*/

    View Slide

  48. https://vimeo.com/ondemand/less/101677664
    https://vimeo.com/channels/everythinganimated
    https://vimeo.com/152376671
    https://www.youtube.com/watch?v=RvYmwo7Tlu0

    View Slide

  49. Javascript The Good Parts (Chap 7) - Douglas Crockford
    RegExer - RegEx Tester and Editor for Javascript
    http://regexr.com/
    Eloquent Javascript (Chap 9) - Marijn Haverbeke
    http://eloquentjavascript.net/09_regexp.html
    MDN (Mozilla Developer Network) on Regular Expressions
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/
    Regular_Expressions
    Josiah’s Github
    https://github.com/kickinbahk/
    Regular_Expressions_LV_DotNet_Meetup_01-2016

    View Slide