Return to Video

01-41 Escaping The Escape

  • 0:00 - 0:03
    Let's put our hard one knowledge of these new regular expressions
  • 0:03 - 0:08
    like any character except new line or set compliment of what we've been talking about
  • 0:08 - 0:10
    to the test.
  • 0:10 - 0:14
    I'd like you to submit via the interpreter, assign to the variable regexp,
  • 0:14 - 0:20
    a Python regular expression that matches double-quoted string literals
  • 0:20 - 0:25
    and--this is the sticker--allows for escaped double quotes.
  • 0:25 - 0:27
    Let me just jump over to the interpreter briefly to show you what I'm talking about,
  • 0:27 - 0:29
    and then we'll come back here.
  • 0:29 - 0:33
    It turns out that there is 1 of those gritty details in this problem that makes life fun,
  • 0:33 - 0:35
    but also complicated the first time.
  • 0:35 - 0:39
    The sorts of strings that I want you to accept are like this one down here.
  • 0:39 - 0:46
    "You say, and then there's a \yes\, I say, no.
  • 0:46 - 0:50
    We've got \no\ and then it ends.
  • 0:50 - 0:55
    This is a well-formed, well-balanced string literal with double quotes
  • 0:55 - 0:56
    and some escaped double quotes.
  • 0:56 - 1:02
    However, to get it to Python, remember that Python is going to treat the escaped sequences,
  • 1:02 - 1:05
    meaning no literally the next thing.
  • 1:05 - 1:08
    But what if you want to literally have a backslash?
  • 1:08 - 1:11
    Then you need to escape the escape sequence.
  • 1:11 - 1:13
    It is turtles all the way down, my friends.
  • 1:13 - 1:16
    So if you want to do some testing on your procedure,
  • 1:16 - 1:20
    here this string 1, this is what you'd have to enter into Python
  • 1:20 - 1:24
    in order to get it to be the sort of string we're looking for.
  • 1:24 - 1:29
    Here I started with single quotes, and then I've double escaped this backslash,
  • 1:29 - 1:31
    then the double quotes, double escape again.
  • 1:31 - 1:35
    Here I've added 2 more, just to make this extra clear.
  • 1:35 - 1:40
    All 3 of these are positive examples--whoops! I'm so wrong!
  • 1:40 - 1:44
    I have forgotten the closing double quote at the end of '"I say, \\"hello.\\"'.
  • 1:44 - 1:47
    This is a negative example.
  • 1:47 - 1:51
    Let's go fix it. There we go. Add in another quote.
  • 1:51 - 1:55
    Now all 3 of these are positive examples of the sorts of strings you'd want to match.
  • 1:55 - 1:58
    You say, yes. I say, no.
  • 1:58 - 2:00
    I do realize that there should be a quote here,
  • 2:00 - 2:03
    but I've intentionally left out the apostrophe in don't,
  • 2:03 - 2:07
    so as not to confuse the issue since we're already talking about quoted strings.
  • 2:07 - 2:11
    So here just to remind you, I've written out a positive example and a negative example.
  • 2:11 - 2:15
    On the left is what you'd have to say to Python and on the right is what it means.
  • 2:15 - 2:19
    And the big hints are that you'll probably want to escape the double quotes and the backslash,
  • 2:19 - 2:21
    just like we did here.
  • 2:21 - 2:26
    You may want to consider having parenthesis nested inside other parenthesis.
  • 2:26 -
    This one's tricky. Good luck!
タイトル:
01-41 Escaping The Escape
概説:

more » « less
Video Language:
English
Team:
Udacity
プロジェクト:
CS262 - Programming Languages
Duration:
02:29
Amara Bot added a translation

English subtitles

改訂