-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregex.py
More file actions
17 lines (15 loc) · 676 Bytes
/
regex.py
File metadata and controls
17 lines (15 loc) · 676 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import re
message = "some message having 123-555-999 nummbers. Call 111-333-111"
# regex basics till line 20
# we pass raw string to compile()
# call re.compile() function to create regex object
phoneregex = re.compile(r'\d\d\d-\d\d\d-\d\d\d')
# d for digit
match_object = phoneregex.search(message)
# search() creates a match object as the name depicts
print(match_object.group())
# group method to get matched string
# it returns the digits above which is so cool
# now this method of search() returns the first occurance, we need more.
print(phoneregex.findall('Here goes my number 555-999-111 and 999-000-777'))
# returns all the occurances found in a string in a list