FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
PythonLearning/67.RegularExpressionExample.py at Pythontuts · aditya0035/PythonLearning · GitHub
aditya0035
/
PythonLearning
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
PythonLearning
/
67.RegularExpressionExample.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
51 lines (42 loc) · 1.59 KB
Breadcrumbs
PythonLearning
/
67.RegularExpressionExample.py
Copy path
File metadata and controls
51 lines (42 loc) · 1.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
re
string
=
'My 2 Favorates Numbers are 19 and 42'
print
(
string
)
y
=
re
.
findall
(
"[0-9]+"
,
string
)
print
(
y
)
#here it will extract data from string 2,19,42
#string start with implementation using search
x
=
"From:Aditya ..........."
if
re
.
search
(
"^From"
,
x
):
print
(
"Match Found"
)
y
=
"Start With XYZ"
if
re
.
search
(
"With"
,
y
):
print
(
"Match Found"
)
"Split function Implementation Using Regex"
commaSepratedString
=
"Aditya,MIT,30"
lstOfString
=
re
.
findall
(
"([A-z 0-9 \s]+)"
,
commaSepratedString
)
print
(
lstOfString
)
"""
Greedy Matching
"""
x
=
"From:Using the:Character"
y
=
re
.
findall
(
"^F.+:"
,
x
)
print
(
y
)
#here it produces the output as greedy as there are two possible strings to fecth
"""
['From:Using the:']
['From:']
but here it has fetched ['From:Using the:'] so these * and + are greedy regex character
as in regex if we didn't say anything the regex lib attempt to give the largest possible version of the string
so to fix that to non greedy search we can use +?,*? which make us to give the first matched string
"""
y
=
re
.
findall
(
"^F.+?:"
,
x
)
print
(
y
)
email
=
"from:stephan.marquard@uct.ac.za"
y
=
re
.
findall
(
"^from:(\S+@\S+)"
,
email
)
#here it will gives the group value as output the parentheses are not the part of match but they tell where to start and
#end in order to extract the string
print
(
y
)
"""
Double Split pattern
Sometimes we split a line one way then grasp one of the piece of the line and split that piece again
Escape Character:
if we want a special regular expression character to just behave normally then we have to prefix that with \ character
"""
Back
|
FazBrowse Home
|
New Git URL