您在這裡

Bonus section for Unix users

24 二月, 2015 - 10:32

Support for searching files using regular expressions was built into the Unix operating system since the 1960’s and it is available in nearly all programming languages in one form or another.

As a matter of fact, there is a command-line program built into Unix called grep (Generalized Regular Expression Parser) that does pretty much the same as the search() examples in this chapter. So if you have a Macintosh or Linux system, you can try the following commands in your command line window.

$ grep 'ˆFrom:' mbox-short.txtFrom: stephen.marquard@uct.ac.zaFrom: louis@media.berkeley.eduFrom: zqian@umich.eduFrom: rjlowe@iupui.edu

This tells grep to show you lines that start with the string “From:” in the file mbox-short.txt. If you experiment with the grep command a bit and read the documentation for grep, you will find some subtle differences between the regular expression support in Python and the regular expression support in grep. As an example, grep does not support the non-blank character “\S” so you will need to use the slightly more complex set notation “[ˆ ]”- which simply means - match a character that is anything other than a space.