Repository navigation
Expand file tree
/
Copy pathstrings.py
More file actions
144 lines (101 loc) · 4.66 KB
/
Copy pathstrings.py
File metadata and controls
144 lines (101 loc) · 4.66 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import re
string = "The Day123 was go0od.!?"
# extract lowercase only
lwr_wrds = re.sub("([^a-z])+", " ", string)
# make letters only
ltrs = re.sub("([^a-zA-Z])+", " ", string)
# make numbers only
nbrs = re.sub("([^0-9])+", " ", string)
# make punctuation only
#punc = re.compile("[[:punct:]]")
punc = re.compile("[!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]")
punc = punc.findall(string)
# can also be written
punc = re.findall(r"[!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]", string)
# make numbers and letters only
nbrs_ltrs = re.sub("([^0-9a-zA-Z])+", " ", string)
# make lowercase
lwr = string.lower()
# make uppercase
upr = string.upper()
# split after fullstop
split_string = str.split(string, ".")
# capitalise only the first word
#cap = str.title(string)
cap = '. '.join(list(map(lambda x: x.strip().capitalize(), string.split('.'))))
################################################################################
string1 = "The.Day//Was1111111Good for the 10 of the month being 10/03/2024 a Sunday"
### extract everything after the // ###
ext = re.sub("\\W\\W+", " ", string1)
# or
ext = re.compile("(?://{1,})([^*]+)")
#(?://{1,}) # find 1 or more instances of //
#([^*]+) # extract everything after this
ext = ext.findall(string1)
# extract date only
date = re.compile("\\d+/\\d+/\\d+")
date = date.findall(string1)
# or
date_2 = re.compile("(?://{1,})*(\d+/\d+/\d+)")
#(?://{1,}) # find 1 or more instances of //
#(\d+/\d+/\d+) # extract numbers in the format d/d/d after this
date_2 = date_2.findall(string1)
# get numbers not joined to any words
std_nm = re.compile("\\s\\d+\\s")
std_nm = std_nm.findall(string1)
# or
std_nm2 = re.findall(r"(?://{1,})*\s(\d+)\s",string1)
#(?://{1,}) # find 1 or more instances of //
#\s(\d+)\s # extract numbers in the format whitespace number whitespace
################################################################################
string2 = "The.Weather.is.nice.today./12abcdef"
# remove fullstops and ignore everything after the /
simple = re.findall("([^.]+)(?:\.)", string2)
# ([^.]+) # extract everything and ignore any . characters
# (?:\.) # do not capture anything after the /
# extract the number after the /
num_simple = re.compile("\\/([0-9]+)")
num_simple = num_simple.findall(string2)
# extract 2 letter words
two_ltr = re.findall(r"(\b\w{2,2}\b)", string2)
# (\b\w{2,2}\b) # find only whole words at word boundary \b and only show those of length 2 characters
################################################################################
string3 = "thezzzzzzzzz day \\\\\ is be goood cc days"
# extract 2 letter words after the \\\\\
#ext1 = re.findall(r"(?:\\{1,})*\s(\w{2,2})(?=\s|$)",string3)
# (?:\\{1,})* #
# \s(\w{2,2}) #
# (?=\s|$) #
ext1 = re.findall(r"\b\w{2,2}\b", string3)
# only return whole words with the letter a in them
a = re.findall(r"(?=[a-z]*a)[a-z]+", string3)
# (?<![a-z]) # negative lookbehind # not needed
# (?=[a-z]*a) # positive lookahead to any words with a in them
# [a-z]+ # capture any words matching this condition
# extract only non duplicated letter words
non_dupe_ltr = re.findall(r"\b(?!\w*?(\w)\1)(\w+)", string3)
# find instances of repeated letters in the string
rptd_ltrs = re.findall(r"(\w+)\1", string3)
################################################################################
string4 = "The summary report was useful. The report had samples of data."
# find the first occurrence of report or sample, case insensitive
# which is separated by a word boundary, or begins with a number and then a word boundary
sngl_match = re.findall(r"\b([Rr]eport|[Ss]ample).*\b.*|.*\d+.*\b([Rr]eport|[Ss]ample)\b.*", string4)
################################################################################
string5 = r"C:\Users\kelvi\Documents\file.csv"
# find any match to a letter "k" and return the entire string between \
k_match = re.findall(r"(?=k[\w+]).*?(?=\\|\/|(?=$))", string5)
# extract the filename
file_name = re.findall(r"[^\\]+(?=\.|$)", string5)
################################################################################
string6 = "1. The day: The day was good. 2. The next day: The next day was 26.2C."
# find the numbers (with the fullstop) before the :
nbr_match = re.findall("(\d+)(?=\D+\:)", string6)
################################################################################
string7 = r"C:\\Folder\File.xyz" # read folder location and file
# get the file extension
file_ext = re.findall("(\.[^.]+)$", string7)
# get the filename
filename = re.findall("([\w]+(?!\\\)\.\w+)$", string7) # \\\ is \
# get the folder
folder = re.findall("(\w+.*)(?<=\\\)", string7)