welcome
play

Welcome IN TERMEDIATE REGULAR EX P RES S ION S IN R Angelo Zehr - PowerPoint PPT Presentation

Welcome IN TERMEDIATE REGULAR EX P RES S ION S IN R Angelo Zehr Data Journalist Where you might have left off INTERMEDIATE REGULAR EXPRESSIONS IN R From Rebus to writing custom expressions Does "cat" start with "c" ? The


  1. Welcome IN TERMEDIATE REGULAR EX P RES S ION S IN R Angelo Zehr Data Journalist

  2. Where you might have left off INTERMEDIATE REGULAR EXPRESSIONS IN R

  3. From Rebus to writing custom expressions Does "cat" start with "c" ? The rebus way: str_detect("cat", pattern = START %R% "c") Regular expression: str_detect("cat", pattern = "^c") INTERMEDIATE REGULAR EXPRESSIONS IN R

  4. Prerequisites: stringr str_detect(string, pattern) str_match(string, pattern) INTERMEDIATE REGULAR EXPRESSIONS IN R

  5. What regular expressions will help you achieve INTERMEDIATE REGULAR EXPRESSIONS IN R

  6. What regular expressions will help you achieve INTERMEDIATE REGULAR EXPRESSIONS IN R

  7. Our �rst dataset movie_titles <- c( movie_titles[ "Karate Kid", str_detect( "The Twilight Saga: Eclispe", movie_titles, "Knight & Day", pattern = "^K" "Shrek Forever After (3D)", ) "Marmaduke.", ] "Predators", "StreetDance (3D)", "Karate Kid", "Robin Hood", "Knight & Day", "Micmacs A Tire-Larigot", ... "Sex And the City 2", ... INTERMEDIATE REGULAR EXPRESSIONS IN R

  8. Special characters in regular expressions Special character Meaning Caret : Marks the beginning of a line or string ^ Dollar Sign : Marks the end of a line or string $ Period : Matches anything: letters, numbers or white spaces . Two backslashes : Escapes the period when we search an actual period \\. INTERMEDIATE REGULAR EXPRESSIONS IN R

  9. For example Code Result Will match "B" str_match("Book", "^.") Will match "k" str_match("Book", ".$") No match str_match("Book", "\\.") Will match "." str_match("Book.", "\\.") INTERMEDIATE REGULAR EXPRESSIONS IN R

  10. Let's practice! IN TERMEDIATE REGULAR EX P RES S ION S IN R

  11. Character classes and repetitions IN TERMEDIATE REGULAR EX P RES S ION S IN R Angelo Zehr Data Journalist

  12. Available character classes Character Class Example \\d or [:digit:] 0, 1, 2, 3,… \\w or [:word:] a, b, c…, 1, 2, 3…, _ [A-Za-z] or [:alpha:] A, B, C,…, a, b, c,… either a , e , i , o or u [aeiou] " " , tabs or line breaks \\s or [:space:] INTERMEDIATE REGULAR EXPRESSIONS IN R

  13. A concrete example Result str_match_all() "3" , "5" "Hi John_35", "\\d" "H" , "i" , "J" , "o" , "h" , "n" , "_" , "3" , "5" "Hi John_35", "\\w" "H" , "i" , "J" , "o" , "h" , "n" "Hi John_35", "[A-Za-z]" "i" , "o" "Hi John_35", "[aeiou]" "Hi John_35", "\\s" " " INTERMEDIATE REGULAR EXPRESSIONS IN R

  14. Repetitions Syntax Meaning exactly 2 times \\w{2} minimum 2 times, maximum 3 times \\w{2,3} minimum 2 times, but no maximum \\w{2,} 1 or more repetitions \\w+ 0, 1 or more repetitions \\w* INTERMEDIATE REGULAR EXPRESSIONS IN R

  15. Inversion of character classes Original Negation \\d match digits \\D match all but digits \\w match word characters \\W match all but word characters \\s match spaces \\S match all but spaces [a-zA-Z] match alphabet [^a-zA-Z] match all but alphabet INTERMEDIATE REGULAR EXPRESSIONS IN R

  16. Custom pattern with classes str_match_all("Toy Story 3", "[\\d\\s]") Result: [,1] [1,] " " [2,] " " [3,] "3" INTERMEDIATE REGULAR EXPRESSIONS IN R

  17. Let's practice! IN TERMEDIATE REGULAR EX P RES S ION S IN R

  18. The pipe and the question mark IN TERMEDIATE REGULAR EX P RES S ION S IN R Angelo Zehr Instructor

  19. This or that lines <- c( "Karate Kid 2, Distributor: Columbia, 58 Screens", "Finding Nemo, Distributors: Pixar and Disney, 10 Screens", "Finding Harmony, Distributor: Unknown, 1 Screen", "Finding Dory, Distributors: Pixar and Disney, 8 Screens" ) str_detect(lines, "Columbia|Pixar") TRUE TRUE FALSE TRUE INTERMEDIATE REGULAR EXPRESSIONS IN R

  20. Making things optional str_view(lines, pattern = "Distributor|Distributors") str_view(lines, pattern = "Distributors?") INTERMEDIATE REGULAR EXPRESSIONS IN R

  21. Greedy vs. lazy str_view("Toy Story 3 In Disney Digital 3D", ".*3") str_view("Toy Story 3 In Disney Digital 3D", ".*?3") INTERMEDIATE REGULAR EXPRESSIONS IN R

  22. Let's practice! IN TERMEDIATE REGULAR EX P RES S ION S IN R

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend