-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreg-ex.py
More file actions
58 lines (51 loc) · 1.51 KB
/
Copy pathreg-ex.py
File metadata and controls
58 lines (51 loc) · 1.51 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import re
email = 'sridhar.smartans@gmail.com'
expression = '[a-z]+' # match characters - many times
matches = re.findall(expression, email)
print(matches)
name = f'{matches[0]}.{matches[1]}'
domain = f'{matches[2]}.{matches[3]}'
print(name)
print(domain)
expression = '[a-z\.]+' # match characters or a dot - many times
matches = re.findall(expression, email)
print(matches)
name = matches[0]
domain = matches[1]
print(name)
print(domain)
# with slpit
parts = email.split('@')
print(parts)
print(parts[0], parts[1])
print("--------")
price = 'Price: $123.50'
expression = '123.50'
matches = re.search(expression, price)
print(matches.group(0))
# print(matches.group(1))
expression = 'Price: \$123.50'
matches = re.search(expression, price)
print(matches.group(0))
#print(matches.group(1))
#--------------
expression = 'Price: \$(123.50)'
matches = re.search(expression, price)
print(matches.group(0)) # entire match
print(matches.group(1)) # first thing in brackets
#--------------
expression = 'Price: \$([0-9]*\.[0-9]*)'
matches = re.search(expression, price)
print(matches.group(0)) # entire match
print(matches.group(1)) # first thing in brackets
price_num = float(matches.group(1))
print(price_num)
#--------------
price = 'Price: $12,3456.50'
expression = 'Price: \$([0-9,]*\.[0-9]*)' # 12,345.50
matches = re.search(expression, price)
print(matches.group(0)) # entire match
print(matches.group(1)) # first thing in brackets
price_without_comma = matches.group(1).replace(',', '')
price_num = float(price_without_comma)
print(price_num)