-
Notifications
You must be signed in to change notification settings - Fork 0
/
regex.py
56 lines (45 loc) · 1.44 KB
/
regex.py
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
#! python3
# regex.py - learning regex
import re
'''
text='my number is 34-675 please note it...'
regex=re.compile(r'\d\d-\d\d\d')
result=regex.search(text)
print(result.group())
text='separate +91-378 as country code and number'
regex=re.compile(r'(\+\d\d)-(\d\d\d)')
result=regex.search(text)
print(result.group()) #print(result.group(1)) , print(result.group(2))
text='my name is hena sometimes called montena'
regex=re.compile(r'hena|montena')
result=regex.search(text)
print(result.group())
text='batman batwoman batchild'
regex=re.compile(r'bat(man|woman|child)')
result=regex.search(text)
print(result.group())
text = 'Where is the batwowowoman?'
regex = re.compile(r'bat(wo)*man') # 0 or more occurence
result = regex.search(text)
print(result.group())
regex1 = re.compile(r'bat(wo)+man') # 1 or more occurence
result = regex1.search(text)
print(result.group())
text = 'where is the batman'
regex2 = re.compile(r'bat(wo)?man') #0 or 1 occurence
result = regex2.search(text)
print(result == None)
#print(result.group())
text = 'where is the batwowowowowoman'
regex = re.compile(r'bat(wo){3,5}man') # greedy(return longest string)
result = regex.search(text)
print(result.group())
text = 'wowowowowo'
regex = re.compile(r'{3,5}?') #non greedy
result = regex.search(text)
print(result.group())
text = 'my work number is 234-645-645 and house number is 987-234-765'
regex = re.compile(r'\d\d\d-\d\d\d-\d\d\d')
result = regex.findall(text)
print(result)
'''