-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex.py
More file actions
executable file
·32 lines (23 loc) · 832 Bytes
/
regex.py
File metadata and controls
executable file
·32 lines (23 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/python
# import regular expressions
import re
import sys
# Open file
f = open(sys.argv[1], 'r')
# findall() in file; returns a list of found strings
strings = re.findall(r'\# ', f.read())
# Print results
for string in strings:
print string
"""
Repetition
Things get more interesting when you use + and * to specify repetition in the
pattern
+ -- 1 or more occurrences of the pattern to its left, e.g. 'i+' = one or more i's
* -- 0 or more occurrences of the pattern to its left
? -- match 0 or 1 occurrences of the pattern to its left; also used to modify . and + to make them nongreedy
Leftmost & Largest
First the search finds the leftmost match for the pattern, and second it tries
to use up as much of the string as possible -- i.e. + and * go as far as
possible (the + and * are said to be "greedy").
"""