-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex.py
72 lines (56 loc) · 1.76 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import re
string = '"I AM NOT EATING THIS " , she said. Though we know she is'
new = re.sub('[A-Z]'," ",string) #sub is substitute
print(new)
new = re.sub('[,/""]'," ",string)
print(new)
new = re.sub('[A-Z]'," ",string)
print(new)
new = re.sub('[.,\ a-z A-Z]'," ",string)
print(new)
new = re.sub('[.,\ A-Z + " "]',"",string)
print(new)
#Check if the string starts with "The" and ends with "Spain":
txt = "The rain in Spain"
x = re.search("^The.*Spain$", txt)
if (x):
print("YES! We have a match!")
else:
print("No match")
#################
'''findall function returns a list containing all matches,
if no match is found an empty list returns'''
str = "The rain in Spain"
x = re.findall("ai", str)
print(x)
#################
'''The search() function searches the string for a match,
and returns a Match object if there is a match.
If there is more than one match, only the first occurrence of the match will be returned'''
str = "The rain in Spain"
x = re.search("\s", str)
print(x)
print("The first white-space character is located in position:", x.start())
################
#split function returns a list where the string has been splited
sub = "I study in NED"
y = re.split("\s",sub)
print(y)
################
#span()
#Search for an upper case "S" character in the beginning of a word, and print its position:
str = "The rain in Spain"
x = re.search("S", str)
print(x.span())
################
#group()
#Search for an upper case "S" character in the beginning of a word, and print the word:
str = "The rain in Spain"
x = re.search(r"\bS\w+", str)
print(x.group())
###############
#string()
#The string property returns the search string:
str = "The rain in Spain"
x = re.search("S", str)
print(x.string)