| Line 254: |
Line 254: |
| | print arg | | print arg |
| | </source> | | </source> |
| | + | == Regular Expressions == |
| | + | <nowiki> |
| | + | ^ → Matches the beginning of a line |
| | + | $ → Matches the end of a line |
| | + | . → Matches any character |
| | + | \s → Matches any whitespace |
| | + | \S → Matches any non-whitespace |
| | + | * → Repeats a character 0 or more times |
| | + | *? → Repeats a character 0 or more times (non-greedy) |
| | + | + → Repeats a character 1 or more times |
| | + | +? → Repeats a character 1 or more times (non-greedy) |
| | + | [aeiou] → Matches a single character in the listed set |
| | + | [^XYZ] → Matches a single character NOT in the listed set |
| | + | [a-z0-9] → The set of characters can include a range |
| | + | ( → Indicates where string extraction is to start |
| | + | ) → Indicates where string extraction is to end</nowiki> |
| | + | \ → Escape character |
| | + | === Regular Expression Module === |
| | + | It must be imported at the begining of a program: |
| | + | <source lang="python"> |
| | + | import re |
| | + | </source> |
| | + | * re.search(re_string, string) → similar to find() |
| | + | * re.findall(re_string, string) → similar to find, returns a list |
| | + | * re.match(re_string, string) |
| | | | |
| | + | <source lang="python"> |
| | + | import re |
| | + | hand = open('mbox-short.txt') |
| | + | for line in hand: |
| | + | line = line.rstrip() |
| | + | if re.search('^From:', line); |
| | + | print line |
| | + | </source> |
| | + | |
| | == Files == | | == Files == |
| | == Class == | | == Class == |
| | == MySQLdb == | | == MySQLdb == |
| | == os.system() == | | == os.system() == |