• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Regular Expressions 101
 

Regular Expressions 101

on

  • 2,707 views

 

Statistics

Views

Total Views
2,707
Views on SlideShare
2,574
Embed Views
133

Actions

Likes
4
Downloads
88
Comments
0

2 Embeds 133

http://newdemosales.fastbooking.com 107
http://www.martialcoder.com 26

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

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.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Regular Expressions 101 Regular Expressions 101 Presentation Transcript

  • Regular Expressions 101 Raj Kissu
  • What are Regular Expressions?
  • They describepatterns in strings
  • These patterns can be used to modify strings
  • Invented by Stephen Cole Kleene
  • Idea of RegEx dates back to the 1950s
  • Today, they come indifferent “flavors”
  • PCRE, POSIX Basic &Extended RegEx, ECMA RegEx and loads more!
  • NOTE: RegEx“flavors” are not consistent in implementation
  • Different “flavors” behave differently ...
  • So know your“flavor” before you use it!
  • Why Use It?
  • Cos it’s animportant tool in every coder’s arsenal!
  • Ok, but still ... Why use it???
  • It’s Short, Sweet & F***ing Fast!
  • It can match just about anything!
  • It makes changing large amounts of repetitive text trivial ...
  • ... as long as youcan “see” patterns
  • It Makes You Awesome- in a Geeky way :)
  • Before RegEx Mastery
  • After RegEx Mastery
  • Readily AvailableSupport in programming languages:JavaScript, PHP, PERL, C/C++,etcCommand-line: grep, awk, sedText-editors: VIM, emacs, Notepad++IDEs: Aptana, Netbeans, VisualStudio .NET
  • RegEx BasicsNOTE : Using ECMA (JavaScript) RegEx Flavor!
  • Charactersstandard characters letters : A to Z, a to z numbers : 0 to 9 symbols : !,@,#,%,& etcMatched literally!
  • Meta CharactersSpecial characters : )(][^$.|?*+To match as literals, escape themwith a backslash
  • Character ClassesMatches one and ONLY one character ina set of characters[Aa] : matches either ‘A’ or ‘a’[a-z] : matches any of the lowercasealphabets in the specified range ONCE[^Aa] : matches anything but ‘A’ and‘a’
  • Character ClassesMetacharacters may behave differentlywithin character classes[^red] : matches anything but ‘r’,‘e’ and ‘d’[r^ed] : matches only ‘r’, ‘^’, ‘e’or ‘d’
  • Shorthand Classesd, [0-9]: digitsw, [da-zA-Z_]: alphanumeric or _s or [ t(?:n|rn)] : whitespaceD, W, S : the above BUT negated
  • The Dot CharacterThe Dot (.) character matches anysingle character BUT the newlineSynonymous with [^n] (UNIX/Linux/Mac)as well as [^rn] (Windows)Use it sparingly - it’s expensive
  • AlternationUsing a pipe |, match either the leftor right side of the patternbear|tiger : matches a string thatcontains either “bear” or “tiger”pedo(bea|tige)r : matches a stringthat contains either “pedobear” or“pedotiger”
  • Quantifiers{n} : matches exactly n times{n,} : matches n or more times{n,m} : matches between n and m times* : same as {0,}+ : same as {1,}? : same as {0,1}
  • QuantifiersQuantifiers are greedy<.+> : matches “<div>holy RegEx,Batman!</div>” instead of stopping at“<div>”Add a ? to make it lazy<.+?> : stops at “<div>” in“<div>holy regex!</div>”
  • AnchorsMatches positions instead ofcharacters^ : matches the beginning of a string$ : matches the end of a stringb : matches between a w and a tokenthat’s not a w
  • GroupingsPlacing parentheses around tokensgroups them together : /nyan(cat)/It also provides abackreference : /(cat)1/ matches“cat”OR if you don’t want abackreference : /(?:nyan)(cat)1/matches “nyancatcat” and not“nyancatnyan”
  • LookaheadPositive Lookahead Iron(?=man) : matches “Iron” only if it is followed by “man”Negative Lookahead Iron(?!man) : matches “Iron” only if it is not followed by “man”
  • LookbehindPositive Lookbehind (?<=Iron)man : matches “man” only if it is preceded by “Iron”Negative Lookbehind (?<!Iron)man : matches “man” only if it is not preceded by “Iron”
  • Modifiersalter behavior of the matching mode(differs between tools)/i : case-insensitive match/m : Multi-line mode/g : affects all possible matches,not just the first
  • Q & A
  • ResourcesMastering Regular Expressions -Jeffrey E.F. Friedlhttp://www.regex.infohttp://www.regular-expressions.infohttp://www.rubular.com
  • Thank You!