-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18.regex.py
More file actions
96 lines (68 loc) · 1.85 KB
/
18.regex.py
File metadata and controls
96 lines (68 loc) · 1.85 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# regex:
'''
Regex module:
'''
import re
name = "Ram kumar singh"
searchname = re.search("^Ram.*singh$", name)
print('serachname: ',searchname)
'''
Regex fuction:
1. findall():
2. search()
3. split()
4. sub()
Regex Metachar:
[] => set of char: [a-zA-Z]
\ => \d \f \n
. => r...m
^ => start with
$ => end with
* => zero or more occurance
+ => one or more
{} => exactly numer of occurencse
| => eiter or
() => capture and group
Special Sequences:
\A => return begning char \ARam
\b => return at befining or ending
\B => returns a match where the specifed char are present
\d => find digit(0-9)
\D => ram001
\s => return stirng ram kumar singh
\S => return where sting not have space
\w => return if have string digit underscore
\W => sting does not contain any word char
\Z => return at last or end srting
Sets:
[arn]
[a-n]
[^arn]
[0123]
[0-9]
[0-5][0-9] => 00,02
[a-zA-Z]
[+] => +,*,.,|,(),$,{}
'''
# practice: findall()
message = "This is monday and great day."
search = re.findall('Ram', message)
print(search)
# search()
aserach = re.search("monday",message)
print('aserach: ',aserach)
# split()
splitmessage = re.split('a',message)
print('splitmessage: ',splitmessage)
splitmessage = re.split(' ',message, 1)
print('splitmessage max: ',splitmessage)
# sub()
replace = re.sub('This', 'July', message)
print('replace: ',replace)
'''
Programs:
1. user se input lena hai then find karna hai ki user ne kis possition ke char ko captil letter me likha hai.
2. user input me user name agar male hai to name me Mr. add karna hai aur agar femal hai to Miss. add karna hai. then using sub method replace name with anyother name.
3. user input me search karna hai agar koi programming languge ka name hai to otherwise not found ka message show karna hai.
4. user input me agar programming lanague found hota hai aur uska first char small hai to usko capital karna hai.
'''