-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quantifier.py
45 lines (31 loc) · 1.1 KB
/
Quantifier.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
# some required knowledge '+' =1 or more
# '?' = 0 or 1
# '*' = 0 or more
# '{n,m}' = n to m repetitions(ranges)
import re
testData='The quick brown fox jumps over the lazy dog'
# to get the data from the start
print(re.search('\w+',testData).group())
# output:The
# \W+ will get the space and \w+ will print the next characters
print(re.search('\w+\W+\w+',testData).group())
# output:The quick
# little bit extended version of upper data
print(re.search('\w+\W+\w+\W+\w+\W+\w+',testData).group())
# output:The quick brown fox
# \W+ got all the spaces but \W? will get 0 or 1 space
print(re.search('\w+\W?\w+',testData).group())
# output:The quick
testData2='The quick brown fox'
print(re.search(r'\w+\W?\w+\W+\w+',testData2).group())
# output: The quick brown
testData3='Thee quick brown fox'
# prints the first three characters
print(re.search(r'\w{3}',testData3).group())
# output: The
# prints the first three chars from 0 index to 3 index(excluding)
print(re.search(r'\w{0,3}',testData3).group())
# output: The
# we can use this
print(re.search(r'\w{0,4}\W{0,5}\w+',testData3).group())
# output: Thee quick