• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Regular Expressions Deep Dive

Regular Expressions Deep Dive






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • http://www.wired.com/threatlevel/2010/05/lifelock-identity-theft/
  • A metacharacter is a character with a special meaning to the RegEx engineIt is a super-character with extra powers.RegEx is a DSL – A domain-specific language.
  • Popeye photo copyright Meng63 at Flickr. Used with permission.Examples:Popeye$5.00\\$5\\.00
  • Examples:
  • Multipliers only have meaning to the right of another regex. A multiplier by itself is nonsensical.
  • Examples:Web de(velop|sign)ers ruleb{2}|[[^b]{2}Image copyright @TrasherDave at Flickr. Used with permission.
  • Boundaries never consume a character. They only ensure that the pattern matched is adjacent to a word or line boundary.
  • Positive lookaheadsPositive lookbehindsNegative lookaheadsNegative lookbehinds
  • These will consume the "a" but will not consume the "b"If the string to search is "Austin Powers is from Austin Texas."Austin // Finds the first AustinAustin Texas // Finds Austin Texas at the endAustin(?= Texas) // Finds only the second AustinAustin(?! Powers) // Finds only the first Austin
  • Note: Lookbehinds are not supported by JavaScript … yet

Regular Expressions Deep Dive Regular Expressions Deep Dive Presentation Transcript

  • REGULAREXPRESSIONS DEEPDIVEMore pattern matching than you ever reallycared to ask about
  • Can you identify each of these?• 4246 3151 7734 2234• 12:34 EDT• (800) 867-5309• 457-55-5462• dwight.k.shrute@dundermifflin.com•• http://www.TotsAndNugs.com• 11/16/2012• 16-11-2012
  • Regular expressions are recognizingpatterns in stringsRegex uses metacharacters to represent types or familiesof characters
  • If its not a metacharacter, it just is what itis• Example: I yam what • a I yam. • 1 • T• To make a metacharacter represent itself (aka. lose its special meaning), precede it by a backslash• Example: • . • ^
  • Single character wildcards representexactly one other character•.• [] • ^ • -• n• N• w• W• d• D
  • Multipliers turn one character intomultiples of one character• Generic multipliers use curly braces • {x} • {x,y} • {x,}• Multiplier shortcuts which will save you keystrokes • + • ? • *
  • Groupers and alternation• ()•|
  • Boundaries allow you to match next tosomething significant•^•$• b• B
  • Problem: how do wewrite a regex for arobust password?• Must have at least one lowercase character• Must have at least one uppercase character• Must have at least one digit
  • Lookarounds limit matches• They match a pattern without consuming them• Only make sense in combination with other Regex• Uses "(?= … )" and "(?< …)"• Read "(?= … )" as "… but only when followed by …"• Read "(?< …)" as "… but only when preceded by …"• Examples • Find an x, but only where its followed by a number • Find an x, but only where it isnt followed by y • Find an x, but only where it follows a punctuation mark • Find an x, but only where it doesnt follow whitespace
  • Lookaheadsmatch thingsthat arefollowed by apattern• Positive lookaheads • a(?=b) • find a but only where followed by b• Negative lookaheads • a(?!b) • find a but only where not followed by b
  • Lookbehindsmatch thingsthat arepreceded by apattern• Positive lookbehinds • a(?<=b) • find a but only where preceded by b• Negative lookbehinds • a(?<!b) • find a but only where not preceded by b
  • So lets write that password regex.• (?=.*[a-z])(?=.*d)(?=.*[A-Z]).{6,20}
  • JavaScript support for regex is prettysolid• All basic regex is supported• Advanced regex like multipliers and backslash character sets are supported• Lookaheads are supported. Lookbehinds are not.
  • Conclusion• Regular Expressions are extremely complex, but extremely powerful• Fortunately, if you can break them down theyre very learnable• You have to use
  • Lab• Create our sites registration form• All should validate client-side. User cannot submit until the data clears properly.