import re #Imports the Regular Expression Library
f = open('randomcharacters.txt') #Opens "randomcharacters.txt" from same directory as the script
lineToSearch = ""
for line in f:
lineToSearch += line #Goes through all the lines in the data one by one
pingFinderPattern = re.compile('time=([0-9.]+) ms')
Pattern1 = re.search('time=([0-9.]+) ms', line)
Pattern1 = re.findall('time=([0-9.]+) ms',lineToSearch)
for i in Pattern1: #Question: Is the i in this mean anything special such as Iterations? I couldn't get an answer for that.
print(i)
There are a lot of issues.Naming: You say Pattern1 = re.search(...) . Pattern1 is not a pattern. A pattern is the description of the regular expression, for example:'time=([0-9.]+) ms' And while we are looking at names, the usual way to name variables is to use lower_case_names or lowerCamelCaseNames Upper case names are usually reserved for classes. I would have written match1 = ... It is important that variables and functions have names that make it easy to understand the code. Misusing a name as you did, makes it much more confusing and programmers have enough complexity without having to handle badly named variables.
Compiling regexes and using them: On line 8 you say pingFinderPattern = re.compile('time=([0-9.]+) ms') (I would have named it pingFinderRe since it is not a raw pattern, but a compiled regular expression.) It is good to compile the re,But you never use it!. Lines 9 and 10 should look like match1 = pingFinderRe.<em>search</em>(line) (or ...<em>findall</em>... )
Looping: I think you are trying to look at each line in some file and find lines that are a 'ping' response. Your code does not do that efficiently. Instead, it builds (expensively) a single string that holds all the lines of the file (that happens at line 7 of your code), then it searches the last line of the file (and throws away the result), then findalls the string that holds all the lines. The last two lines print, one per line, all the substrings that match the hard coded pattern at line 10. I believe this is not what you want to do.
Here is a sketch of what you might do instead
import re # use regex package
pingPatternRe =re.compile(...) # compile the pattern I want to recognize
hits = [] # store the hits as I find them
with open(randomcharacters.txt) as f: # open the file so as to ensure it gets closed
for line in f: # loop through every line in the file
match = pingPatternRe.search(line) # look for a match to the pingPattern
if match: # if pingPattern is not there, match is None, otherwise...
hits.append(line.strip()) # store the matching line without the newline
# Unless error, we've seen every line and stored good match lines in the hits list
for h in hits: # loop over the successful matches
print(h) # and print them
I on the other hand, might replace lines 5 through 11 with this less obvious single line, explained below: print("".join([x for x in f if pingPatternRe.search(x)])) [x for x in f...] makes a list of lines in f ...
[ ... if pingPatternRe.search(x)] ... where the search succeeds
"".join([<em>a_list</em>]) merge the items (must each be a string) in [a_list] using the empty string between the items. Note that we kept the newlines when we built the list, so the newlines are still there (see subtle issue below)
print(...) prints the lines using their own newlines
(subtle issue): The line ends in the file might not be the same as the line ends that your console expects, so making use of them when printing is not robust. (It is however, very likely to work for any student project)