Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
×

# Regular Expressions

• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

### Regular Expressions

1. 1. Regular Expressions Satyanarayana D < satyavvd@yahoo-inc.com>
2. 2. Topics <ul><li>What? </li></ul><ul><li>Why? </li></ul><ul><li>History - Who? </li></ul><ul><li>Flavou?rs </li></ul><ul><li>Grammar </li></ul><ul><li>Meta Chars </li></ul><ul><li>Character Classes </li></ul><ul><li>Shorthand Char Classes </li></ul><ul><li>Anchors </li></ul><ul><li>Repeaters or Quantifiers </li></ul><ul><li>Grouping & Capturing </li></ul><ul><li>Alternation </li></ul><ul><li>Match Float </li></ul><ul><li>Atomic Grouping </li></ul><ul><li>Look Around </li></ul><ul><li>Conditional Expr. </li></ul><ul><li>Recursive Regex </li></ul><ul><li>Code Evalution </li></ul><ul><li>Code Expr. </li></ul><ul><li>Inline Modifiers </li></ul><ul><li>Regex Tools </li></ul><ul><li>Q&A </li></ul>
3. 3. What are Regular Expressions? <ul><li>A Regular expression is a pattern describing a certain amount of text. </li></ul><ul><li>A regular expression, often called a pattern, is an expression that describes a set of strings. - Wikipedia </li></ul>
4. 4. Why do we need? <ul><li>Regular expressions allow matching and manipulation of textual data. </li></ul><ul><li>Requirements </li></ul><ul><ul><li>Matching/Finding </li></ul></ul><ul><ul><li>Doing something with matched text </li></ul></ul><ul><ul><li>Validation of data </li></ul></ul><ul><ul><li>Case insensitive matching </li></ul></ul><ul><ul><li>Parsing data ( ex: html ) </li></ul></ul><ul><ul><li>Converting data into diff. form etc. </li></ul></ul>
5. 5. History Stephen Kleene A mathematician discovered ‘ regular sets ’.
6. 6. History Ken Thompson 1968 - Regular Expression Search Algorithm. Qed -> ed -> g/re/p
7. 7. History Henry Spencer 1986 – Wrote a regex library in C
8. 8. Regex Flavors <ul><li>BRE - Basic Regular Expressions </li></ul><ul><ul><li>?, +, {, |, (, and ) </li></ul></ul><ul><ul><li>ed, g/re/p, sed </li></ul></ul><ul><li>ERE - Extended Regular Expressions </li></ul><ul><ul><li>?, +, {, |, (, and ) </li></ul></ul><ul><ul><li>grep –E == egrep, awk </li></ul></ul><ul><li>PCRE - Philip Hazel </li></ul><ul><ul><li>Perl, PHP, Tcl etc. </li></ul></ul>
9. 9. Grammar of Regex * RE = one or more non-empty ‘ branches ‘ separated by ‘|’ Branch = one or more ‘ pieces ’ Piece = atom followed by quantifier Quantifier = ‘*,+,?’ or ‘ bound ’ Bound = atom{n}, atom{n,}, atom {m, n} Atom = (RE) or () or ‘ ^,\$,’ or followed by `^.[\$()|*+?{’ or any-char or ‘ bracket expression ’ Bracket Expression = is a list of characters enclosed in `[ ]'
10. 10. Meta Chars? 2 + 4 Here ‘+’ has some special meaning In a normal Expression like :
11. 11. Meta Chars Quote the next metacharacter ^ Match the beginning of the line . Match any character (except newline) = [^n] \$ Match the end of the line (or before newline at the end) | Alternation ( ) Grouping [ ] Character class { } Match m to n times * Match 0 or more times + Match 1 or more times ? Match 1 or 0 times
12. 12. Non-printable Chars t tab (HT, TAB) n newline (LF, NL) r return (CR) f form feed (FF) a alarm (bell) (BEL) e escape (think troff) (ESC) 033 octal char (example: ESC) x1B hex char (example: ESC) x{263a} long hex char (example: Unicode SMILEY) cK control char (example: VT) N{name} named Unicode character
13. 13. Character Classes – [ ] <ul><li>Set of character placed inside square brackets. Inside brackets meta characters lose their meaning ( except ‘] ^ - ‘) </li></ul><ul><li>Requirements </li></ul><ul><ul><li>Matches one and only one character of a specified chars. </li></ul></ul><ul><ul><li>Range can be specified using ‘-’. </li></ul></ul><ul><ul><ul><li>a-z matches 26 lower case English alphabets </li></ul></ul></ul><ul><ul><ul><li>0-9 matches any digit. </li></ul></ul></ul><ul><ul><ul><li>Negation can be specified using ‘^’ at the beginning of class. </li></ul></ul></ul><ul><ul><ul><li>In order to match above specified exceptional chars literally either escape them or need to specify at end. </li></ul></ul></ul>[0-9] Matches any one of 0,1,2,3,4,5,6,7,8,9. [aeiou] Matches one English vowel char. [^aeiou] Matches any non-vowel char. [a-z-] Matches a to z and ‘-’ [a-z0-9] Union matches a to z and 0 to 9. [a-z&&[m-z]] Intersection matches m to z. [a-z-[m-z] Subtraction matches a to l.
14. 14. POSIX Character Classes – [: … :] [^[:digit:] ]= D = [^0-9]
15. 15. Shorthand Chars w word character [A-Za-z0-9_] d decimal digit [0-9] s whitespace [ nrtf] W not a word character [^A-Za-z0-9_] D not a decimal digit [^0-9] S not whitespace [^ nrtf]
16. 16. Anchors/Assertions <ul><li>Anchor matches a certain position in the subject string and it won’t consume any characters. </li></ul>^ Match the beginning of the line \$ Match the end of the line (or before newline at the end) A Matches only at the very beginning z Matches only at the very end Z Matches like \$ used in single-line mode b Matches when the current position is a word boundary <,> Matches when the current position is a word boundary B Matches when the current position is not a word boundary
17. 17. ^Anchors <ul><li>Anchor matches a certain position in the subject string and it won’t consume any characters. </li></ul>^ Match the beginning of the line Anchor matches a certain position In the subject string and it won’t consume a ny characters /^a/ String begin with ‘a’
18. 18. Anchors\$ <ul><li>Anchor matches a certain position in the subject string and it won’t consume any characters. </li></ul>\$ Match the end of the line (or before newline at the end) Anchor matche s a certain position In the subject string and it won’t consume any character s /s\$/ String end with ‘s’
19. 19. A Anchors <ul><li>Anchor matches a certain position in the subject string and it won’t consume any characters. </li></ul>A Matches only at the very beginning Anchor matches a certain position In the subject string and it won’t consume any characters ^ Vs A
20. 20. z, Z Anchors <ul><li>Anchor matches a certain position in the subject string and it won’t consume any characters. </li></ul>z Matches only at the very end Z Matches like \$ used in single-line mode Anchor matches a certain position In the subject string and it won’t consume any characters \$ Vs z , Z
21. 21. b, B Anchors <ul><li>Anchor matches a certain position in the subject string and it won’t consume any characters. </li></ul>b = Ww|wW = Matches a word boundary B Matches when the current position is not a word boundary /b2b/ /B2B/ \$ xl 2 twiki file 2 > /dev/null
22. 22. Quantifiers <ul><li>Why? – Because we are not sure about text. Specifies how many times regex component must repeat. </li></ul>{m, n} = Matches minimum of m and a max of n occurrences. * = {0,} = Matches zero or more occurrences ( any amount). + = {1,} = Matches one or more occurrences. ? = {0,1} = Matches zero or one occurrence ( means optional ). Quantifiers ( repetition) :
23. 23. Quantifiers <ul><li>By default quantifiers are greedy. </li></ul>/d{2,4}/ 2010 /<.+>/ My first <strong> regex </strong> test. <strong> regex </strong> /w+sion/ Expression If the entire match fails because they consumed too much, then they are forced to give up as much as needed to make the rest of regex succeed
24. 24. Non Greedy Quantifiers {,}? *? +? ?? To make non greedy quantifiers append ‘?’ <.+?> My first <strong> regex </strong> test. <strong> Use negated classes <[^>]+> My first <strong> regex </strong> test. <strong>
25. 25. Grouping – ( ) <ul><li>Why? – To create sub patterns, so that you can apply regex operators to whole sub patterns or you can reference them by corresponding sub group numbers. </li></ul>d{2}-d{2} -d{2} (d{2})? Will match 01-01-10 and 01-01-2010 also. <ul><li>Grouping can be used for alternation. </li></ul>
26. 26. Alternation - | <ul><li>Why? – Lets you to match more than one sub-expression at same point. </li></ul>/b( get | set )Valueb/ Match either getValue or setValue . <ul><li>Branches are tried from left->right. </li></ul><ul><li>Eagerness - Most likely pattern as first alternative </li></ul><ul><ul><li>(and|android) -> ‘robot and an and roid fight’ </li></ul></ul>
27. 27. Capturing – ( ) <ul><li>Allows us to access sub-parts of pattern for later processing. </li></ul><ul><ul><li>All captured sub patterns are stored in memory. </li></ul></ul><ul><ul><li>Captured patterns are numbered from left to right. </li></ul></ul>/b((d{2})-(d{2})-(d{2}(d{2})?))b/ b ( ( d{2} ) - ( d{2} ) - (d{2} ( d{2} ) ?) ) b Today is ‘ 18-08-2010 ’. 1 -> date -> 18-08-2010 2 -> day-> 18 3 -> month -> 08 4 -> year -> 2010 5 -> year -> last two digits -> 10
28. 28. Non-Capturing sub patterns– (?: ) <ul><li>If you really don’t require back referencing make sub expressions as non-capture, It will save memory and processing time. </li></ul>d{2}-d{2} -d{2} (?:d{2})? Will match 01-01-10 and 01-01-2010 also.
29. 29. <ul><li>We can give names for sub patterns instead of numbers. </li></ul>(?P<name>pattern) -> Python Style, Perl 5.12 (?P=name) -> Back reference (?<name>pattern) or (?’name’pattern) ->Perl 5.10 k<name> or k’name’ or -> Back reference g{name} g{-1}, g{-2} -> Relative Back reference. (?<vowel>[ai]).k<vowel>.1 abr acada bra !! /(w+)s+g{-1}/ &quot;Thus joyful Troy Troy maintained the the watch of night...” \$date=&quot;18-08-2010&quot;; \$date =~ s/(?<day>d{2})-(?<month>d{2})-(?<year>d{4})/\$+{year}-\$+{month}-\$+{day}/; Named Capture – (?<> )
30. 30. <ul><li>Hits </li></ul><ul><ul><li>Lines that I want to match. </li></ul></ul><ul><li>Misses </li></ul><ul><ul><li>Lines that I don’t want to match. </li></ul></ul><ul><li>Omissions </li></ul><ul><ul><li>Lines that I didn’t match but wanted to match. </li></ul></ul><ul><li>False alarms </li></ul><ul><ul><li>Lines that I matched but didn’t want to match. </li></ul></ul>Before Evaluating Regex
31. 31. Float number = integerpart.factionalpart Matching a float number Basic Principle – Split your task into sub tasks
32. 32. Integerpart = d+ -> will match one or more digits Matching a float number
33. 33. Matching a float number Literal dot = . Integerpart = d+ -> will match one or more digits
34. 34. Matching a float number Literal dot = . Integerpart = d+ -> will match one or more digits Fractional part= d+ -> will match one or more digits
35. 35. Integerpart = d+ Matching a float number Literal dot = . Fractional part = d+ Combine all of them = d+.d+
36. 36. Matching a float number /d+.d+/ -> Is generic. It won’t match -123.45 or +123.45
37. 37. Matching a float number /d+.d+/ -> Is generic. It won’t match -123.45 or +123.45 /[+-]?d+.d+/ -> will match.
38. 38. Matching a float number But It won’t match - 123.45 or + 123.45 /[+-]?d+.d+/ -> will match. /[+-]? *d+.d+/ -> will match. But It won’t match 123. or .45
39. 39. Matching a float number /[+-]? *(?:d+.d+|d+.|.d+)/ -> will match. But It won’t match 123. or .45 /[+-]? * (?: d+.d+ | d+. | .d+ ) /
40. 40. Matching a float number /[+-]? *(?:d+.d+|d+.|.d+)(?:[eE]d+)?/ -> will match. But It won’t match 10e2 or 101E5 / [+-]? * (?: d+.d+ | d+. | .d+ ) (?: [eE]d+ )? /
41. 41. Matching a float number /^[+-]? *(?:d+.d+|d+.|.d+)(?:[eE][+-]?d+)?\$/ -> will match. But It won’t match 10e-2 / ^[+-]? * (?: d+.d+ | d+. | .d+ ) (?: [eE][+-]?d+ )? \$/x
42. 42. Match a float number /^ [+-]? * # first, match an optional sign (?: # then match integers or f.p. mantissas: d+.d+ # mantissa of the form a.b |d+. # mantissa of the form a. |.d+ # mantissa of the form .b |d+ # integer of the form a ) (?:[eE][+-]?d+)? # finally, optionally match an exponent \$/x;
43. 43. Atomic Grouping – (?> ) <ul><li>Before looking into Atomic grouping need to know about Backtracking. </li></ul><ul><li>Backtracking – If you don’t succeed try and try again... </li></ul>d+99 19999 d 1 9999 -> Add 1 to match -> 1 d+ 19 999 -> Add 9 to match -> 19 d+ 199 99 -> Add 9 to match -> 199 d+ 1999 9 -> Add 9 to match -> 1999 d+ 19999 -> Add 9 to match -> 19999 d+ 19999 -> Still need to match 99 d+ 99 1999 9 -> Give up a 9 d+ 99 199 99 -> Give up one more 9 d+99 19999 -> Success
44. 44. Atomic Grouping – (?> ) <ul><li>Before looking into Atomic grouping need to know about Backtracking. </li></ul><ul><li>Backtracking – If you don’t succeed try and try again... </li></ul>d+xx 199Rs d 1 99Rs -> Add 1 to match -> 1 d+ 19 9Rs -> Add 9 to match -> 19 d+ 199 Rs -> Add 9 to match -> 199 d+x 199 Rs -> x not matched with R d+x 19 9 Rs -> Give up 9, still cannot match x d+x 1 99 Rs -> Give up 9, still cannot match x d+x 1 99 Rs -> Cannot give 1 due to d+ d+xx 199Rs -> Failure
45. 45. Atomic Grouping – (?> ) <ul><li>Atomic Grouping disables backtracking and speeds up the process. </li></ul><ul><ul><li>(?>pattern) here pattern will be treated as atomic token. </li></ul></ul><ul><ul><li>(?>d+)xx here (?>d+) won’t give up any digits and it locks. </li></ul></ul><ul><ul><ul><li>fails right at matching x with R. </li></ul></ul></ul><ul><ul><li>Atomic groups are not captured and can be nested. </li></ul></ul><ul><li>Use Possessive quantifiers for single items to overcome backtracking. </li></ul><ul><ul><li>Adding ‘ + ’ will make quantifier as possessive </li></ul></ul><ul><ul><li>(?>d+)xx == d+ + xx </li></ul></ul>Atomic Grouping: Possessive Quantifiers:
46. 46. Look Around Ahead Behind Positive Negative Positive Negative (?=...) (?!...) (?<=...) (?<!...) (?=...) Zero-width positive lookahead assertion (?!...) Zero-width negative lookahead assertion (?<=...) Zero-width positive lookbehind assertion (?<!...) Zero-width negative lookbehind assertion *Note : Assertions can be nested. Example : /(?<=, (?! (?<=d,)(?=d) ) )/
47. 47. Look Around <ul><ul><li>/cat(?=s+)/ I catch the house cat 'Tom-cat' with catnip </li></ul></ul><ul><ul><li>/(?<=s)catw+/ I cat ch the housecat 'Tom-cat' with cat nip </li></ul></ul><ul><ul><li>/bcatb / I catch the housecat 'Tom- cat ' with catnip </li></ul></ul><ul><ul><li>/(?<=s)cat(?=s)/ no isolated 'cat’ </li></ul></ul>“ I catch the housecat 'Tom-cat' with catnip” <ul><ul><li>/cat(?!s)/ I cat ch the housecat 'Tom- cat ' with cat nip </li></ul></ul><ul><ul><li>/(?<!s)cat/ I catch the house cat 'Tom- cat ' with catnip </li></ul></ul>*Note : look-behind expressions cannot be of variable length. means you cannot use quantifiers (?, *, +, or {1,5}) or alternation of different-length items inside them.
48. 48. Conditional expressions <ul><li>A conditional expression is a form of if-then-else statement that allows one to choose which patterns are to be matched, based on some condition </li></ul><ul><ul><li>(?(condition)yes-regexp)&quot; is like an 'if () {}' statement </li></ul></ul><ul><ul><li>(?(condition)yes-regexp|no-regexp) 'if () {} else {}' statement </li></ul></ul><ul><ul><ul><li>Condition can be </li></ul></ul></ul><ul><ul><ul><ul><li>Sub pattern match corresponding number </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Lookaround Assertion </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Recursive call </li></ul></ul></ul></ul>Match a (quoted)? string -> / ^(&quot;|')?[^”’]*(?(1)1)\$ / Matches 'blah blah’ Matches “blah blah” Matches blah blah Won’t Match ‘blah blah”
49. 49. Conditional expression <ul><li>A conditional expression is a form of if-then-else statement that allows one to choose which patterns are to be matched, based on some condition </li></ul><ul><ul><li>(?(condition)yes-regexp)&quot; is like an 'if () {}' statement </li></ul></ul><ul><ul><li>(?(condition)yes-regexp|no-regexp) 'if () {} else {}' statement </li></ul></ul>/ (.)1(?(<=AA)G|C)\$ / ATGAAG TAGBBC GATGGC /usr/share/dict/words -> / ^(.+)(.+)?(?(2)21|1)\$ / aa baba beriberi maam vetitive
50. 50. Recursive Patterns – (?) <ul><li>(x(x)y(x)x) </li></ul><ul><li>Palindrome -> /^((.)(?:(?1)|w)*(2))\$/ </li></ul><ul><ul><li>qr/ </li></ul></ul><ul><ul><li>^ # Start of string </li></ul></ul><ul><ul><li>( # Start capture group 1 </li></ul></ul><ul><ul><li>( # Open paren </li></ul></ul><ul><ul><li>(?> # Possessive capture subgroup </li></ul></ul><ul><ul><li>[^()]++ # Grab all the non parens we can </li></ul></ul><ul><ul><li>| # or </li></ul></ul><ul><ul><li>(?1) # Recurse into group 1 </li></ul></ul><ul><ul><li>)* # Zero more times </li></ul></ul><ul><ul><li>) # Close Paren </li></ul></ul><ul><ul><li>) # End capture group 1 </li></ul></ul><ul><ul><li>\$ # End of string </li></ul></ul><ul><ul><li>/x; </li></ul></ul>
51. 51. Code Evaluation – (?{ }) <ul><li>Perl code can be evaluated inside regular expressions using </li></ul><ul><ul><li>(?{ }) construct. </li></ul></ul>\$x = &quot;aaaa”; \$x =~ /(a(?{print &quot;Yown&quot;;}))*aa/; produces Yow Yow Yow Yow
52. 52. Pattern Code Expression – (??{ }) <ul><li>Pattern code expression - the result of the code evaluation is treated as a regular expression and matched immediately. </li></ul><ul><ul><li>Construct is (??{ }) </li></ul></ul>\$length = 5; \$char = 'a'; \$str = 'aaaaabb'; \$str =~ /(??{\$char x \$length})/x; # matches, there are 5 of 'a'
53. 53. Inline modifiers & Comments Matching can be modified inline by placing modifiers. (?i) enables case-insensitive mode (?m) enables multiline matching for ^ and \$ (?s) makes dot metacharacter match newline also (?x) ignores literal whitespace (?U) makes quantifiers ungreedy (lazy) by default \$answers =~ / (?i) y (?-i) (?:es)?/ -> Will match ‘y’, ’Y’, ’yes’, ’Yes’ but not ‘YES’. Comments can be inserted inline using (?#) construct. /^ (?#begin) d+ (?#match integer part) . (?#match dot) d+ (?#match fractional part) \$/
54. 54. Regex Testers Tools Editors Vim, TextMate, Edit Pad Pro, NoteTab, UltraEdit RegexBuddy Reggy – http:// reggyapp.com http://rubular.com (Ruby) RegexPal (JavaScript) - http://www.regexpal.com http://www.gskinner.com/RegExr/ http://www.spaweditor.com/scripts/regex/index.php http://regex.larsolavtorvik.com/ (PHP, JavaScript) http://www.nregex.com/ ( .NET ) http://www.myregexp.com/ ( Java ) http://osteele.com/tools/reanimator ( NFA Graphic repr. ) Expresso - http://www.ultrapico.com/Expresso.htm ( .NET ) Regulator - http://sourceforge.net/projects/regulator ( .NET ) RegexRenamer - http://regexrenamer.sourceforge.net/ ( .NET ) PowerGREP http://www.powergrep.com/ Windows Grep - http://www.wingrep.com/
55. 55. Regex Resources \$perldoc perlre perlretut perlreref \$man re_format “ Mastering Regular Expressions” by Jeffrey Friedl http://oreilly.com/catalog/9780596528126/ “ Regular Expressions Cookbook” by Jan Goyvaerts & Steven Levithan http://oreilly.com/catalog/9780596520694
56. 56. Questions? * { } ^ ] + \$ [ ( ? . ) - : #
57. 57. Thank Y!ou * { } ^ ] + \$ [ ( ? . ) - : #
58. 58. Java Regex <ul><li>import java.util.regex.*; </li></ul><ul><li>public class MatchTest { </li></ul><ul><li>public static void main(String[] args) throws Exception { </li></ul><ul><li>String date = &quot;12/30/1969&quot;; </li></ul><ul><li>Pattern p =Pattern.compile(&quot;^(dd)[-/](dd)[-/](dd(?:dd)?\$&quot;); </li></ul><ul><li>Matcher m = p.matcher(date); </li></ul><ul><li>if (m.find( )) { </li></ul><ul><ul><ul><li>String month = m.group(1); </li></ul></ul></ul><ul><ul><ul><li>String day = m.group(2); </li></ul></ul></ul><ul><ul><ul><li>String year = m.group(3); </li></ul></ul></ul><ul><ul><ul><li>System.out.printf(&quot;Found %s-%s-%sn&quot;, year, month, day); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><li>} </li></ul><ul><li>} </li></ul>
59. 59. PHP Regex <ul><li>\$date = &quot;12/30/1969&quot;; </li></ul><ul><li>\$p = &quot;!^( dd)[-/](dd)[-/](dd(?:dd)?)\$ !&quot;; </li></ul><ul><li>if (preg_match(\$p,\$date,\$matches) { </li></ul><ul><ul><li>\$month = \$matches[1]; </li></ul></ul><ul><ul><li>\$day = \$matches[2]; </li></ul></ul><ul><ul><li>\$year = \$matches[3]; </li></ul></ul><ul><li>} </li></ul><ul><li>\$text = &quot;Hello world. <br>&quot;; </li></ul><ul><li>\$pattern = &quot;{<br>}i&quot;; </li></ul><ul><li>echo preg_replace(\$pattern, &quot;<br />&quot;, \$text); </li></ul>
60. 60. JavaScript Regex <ul><li>var date = &quot;12/30/1969&quot;; </li></ul><ul><li>var p =new RegExp(&quot;^( dd)[-/](dd)[-/](dd(?:dd)?)\$ &quot;); </li></ul><ul><li>var result = p.exec(date); </li></ul><ul><li>if (result != null) { </li></ul><ul><ul><li>var month = result[1]; </li></ul></ul><ul><ul><li>var day = result[2]; </li></ul></ul><ul><ul><li>var year = result[3]; </li></ul></ul><ul><li>} </li></ul><ul><li>String text = &quot;Hello world. <br>&quot;; </li></ul><ul><li>var pattern = /<br>/ig; </li></ul><ul><li>test.replace(pattern, &quot;<br />&quot;); </li></ul>
61. 61. .NET Regex <ul><li>using System.Text.RegularExpressions; </li></ul><ul><li>class MatchTest { </li></ul><ul><li>static void Main( ) { </li></ul><ul><ul><li>string date = &quot;12/30/1969&quot;; </li></ul></ul><ul><ul><li>Regex r = </li></ul></ul><ul><ul><li>new Regex( @&quot;^(dd)[-/](dd)[-/](dd(?:dd)?)\$&quot; ); </li></ul></ul><ul><ul><li>Match m = r.Match(date); </li></ul></ul><ul><ul><li>if (m.Success) { </li></ul></ul><ul><ul><ul><li>string month = m.Groups[1].Value; </li></ul></ul></ul><ul><ul><ul><li>string day = m.Groups[2].Value; </li></ul></ul></ul><ul><ul><ul><li>string year = m.Groups[3].Value; </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
62. 62. Python Regex <ul><li>import re </li></ul><ul><li>date = '12/30/1969’ </li></ul><ul><li>regex = re.compile(r'^(dd)[-/](dd)[-/](dd(?:dd)?)\$') </li></ul><ul><li>match = regex.match(date) </li></ul><ul><li>if match: </li></ul><ul><ul><li>month = match.group(1) #12 </li></ul></ul><ul><ul><li>day = match.group(2) #30 </li></ul></ul><ul><ul><li>year = match.group(3) #1969 </li></ul></ul>
63. 63. Ruby Regex <ul><li>date = '12/30/1969’ </li></ul><ul><li>regexp = Regexp.new('^(dd)[-/](dd)[-/](dd(?:dd)?)\$') </li></ul><ul><li>if md = regexp.match(date) </li></ul><ul><ul><li>month = md[1] #12 </li></ul></ul><ul><ul><li>day = md[2] #30 </li></ul></ul><ul><ul><li>year = md[3] #1969 </li></ul></ul><ul><li>end </li></ul>
64. 64. Unicode Properties
65. 65. Pattern Code Expression – (??{ }) <ul><li>Pattern code expression - the result of the code evaluation is treated as a regular expression and matched immediately. </li></ul><ul><ul><li>(??{ }) </li></ul></ul>Find Incremental numbers ? \$str=&quot;abc 123 hai cde 34567 efg 1245 a132 123456789 10adf&quot;; print &quot;\$1n&quot; while(\$str=~/D ( (d) (?{\$x=\$2}) ( (??{++\$x%10}) )* ) D/gx);'
66. 66. Commify a number \$no=123456789; substr(\$no,0,length(\$no)-1)=~s/(?=(?<=d)(?:dd)+\$)/,/g; print \$no’ Produce 12,34,56,789
67. 67. Find Incremental numbers ? \$str=&quot;abc 123 hai cde 34567 efg 1245 a132 123456789 10adf&quot;; print &quot;\$1n&quot; while(\$str=~/D ( (d) (?{\$x=\$2}) ( (??{++\$x%10}) )* ) D/gx);’ Non Capture group in a capture group won’t work : perl -e '\$x=&quot;cat cat cat&quot;;\$x=~/(cat(?:s+))/;print &quot;:\$1:&quot;;’

### Be the first to comment

• #### fenglq02

May. 25, 2012
• #### hutusi

Jan. 12, 2013

Mar. 3, 2013
• #### MrBassam

Apr. 29, 2013
• #### masud_cse_05

Dec. 18, 2013
• #### slkn8276

Oct. 26, 2014

Dec. 1, 2014
• #### pfodik

Dec. 23, 2014
• #### jayant4u

Mar. 30, 2015

Jul. 8, 2015

Dec. 6, 2015
• #### allenjr1076

Mar. 15, 2016
• #### DesmondTam

May. 22, 2017

Oct. 3, 2017
• #### vikassolanki4

Oct. 27, 2017

Jul. 9, 2018
• #### ssuserc9378f

Jun. 20, 2019

Total views

13,311

On Slideshare

0

From embeds

0

Number of embeds

18

592

Shares

0

0

Likes

17