English subtitles

← Import Re - Programming Languages

Get Embed Code
5 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. Industrial software is often so big that it doesn't all fit on one page,
  2. so people have to break it up into chunks just like a book is broken up into chapters
  3. or the body of human knowledge is broken up into many books.
  4. In computer science, a module is a repository or a library of code--
  5. functions and data that do useful things.
  6. In Python, import brings in a module.
  7. It turns out that there is already a bunch of functions
  8. related to regular expressions. We won't have to reinvent the wheel.
  9. We can just import them into our own Python programs and get their benefits for free.
  10. Python's regular expression module is called, imaginatively enough, "re"--regular expressions.
  11. At the beginning of a Python program, just write "import space re"
  12. and then you'll have access to all of the regular expression functions.
  13. I'm going to show them to you in just a minute.
  14. If we're going to write down regular expressions in Python, we need to know what they look like.
  15. Python regular expressions look just like strings.
  16. They start with double quotes or single quotes, and they have contents,
  17. except that to separate regular expressions from strings
  18. regular expressions begin with a lowercase "r" outside of the double quotes.
  19. Up here this is a--zero, one, two, three, four--a five-character string 0 through 9.
  20. This one down here that begins with an "r" is a regular expression
  21. that matches 10 different one-letter strings.
  22. Writing regular expressions is a creative process.
  23. You the programmer have to do it.
  24. I'm using "creative" here in the same way that people often describe mathematics as elegant.
  25. Just as there are many different equations that could get you the number 4--
  26. 2 plus 2, 1 plus 3, 8 divided by 2, absolute value of the square root of 16--
  27. in fact, and infinite number--there are often an infinite number of regular expressions
  28. that could serve a purpose.
  29. Picking the right one, the small one, the simple one, the concise one, the elegant one,
  30. requires creativity. It's a skill. It's something you'll learn in this class.
  31. Let's say you've written a regular expression, though--like this one.
  32. Now we need to use it.
  33. One of the most common functions involving regular expressions is findall.
  34. It takes a regular expression and a string and returns a list of all of the substrings
  35. that match that regular expression.
  36. Here if we're looking for single letter strings that are between 0 and 9,
  37. the 1 matches, the plus does not, the 2 matches, the equals does not,
  38. the equals does not, and the 3 matches.
  39. The return value of re.findall
  40. is a list--1, 2, 3--of all of the substrings that match the regular expression.
  41. The list could be empty if you didn't actually match anything.
  42. This "re" means it comes from the regular expression library.
  43. We really need that import statement at the beginning for this to work.
  44. In this example, I'm using the same haystack string--"1+2==3"--
  45. but I'm using a different regular expression.
  46. This one only matches two single-letter strings.
  47. We'll get out 1 and 2. These two match. The 3 does not, because it's not between 1 and 2.
  48. It's not specified or matched by this regular expression.
  49. This last example is a little more tricky.
  50. We're looking for the letters a through c,
  51. but if you look carefully, these are the lowercase letters a and c.
  52. So even though this "B" is very tempting--this capital "B" in "Barbara,"
  53. it's not between lowercase a and lowercase c.
  54. We'll match this a, b, a, a, and then there's nothing over here in "Liskov."
  55. Barbara Liskov is a well-known computer scientist, famous for her work in
  56. object-oriented programming and systems programming.
  57. Among other things she invented the language Clu,
  58. which I'm showing here with a bit of a magnifying lens.
  59. She received the Turing Award--the highest honor is computer science.