In this example, we used regular expression to implement the strings.startswith() function. The program will ask for users' input on a sub-string to find at the beginning of each string. The program will then open a file and print out the lines that start with the sub-string being specified.
The pattern_str stored the pattern we specified using regular expression. The '^' character allows us to specify that we are looking for the pattern at the beginning of a string. The '^{}'.format(start_str) allows us to generate a string that consists of '^' and the content in the start_str variable. In this case, '^{}'.format(start_str) will generate a string '^Hello' when the content/value of the variable start_str is 'Hello'. By using '^{}'.format(start_str), we can dynamically change the pattern based on users' input.
The re.search( pattern_str, some_str) will search for sub-string in some_str that matches the pattern in psttern_str. If a match is found, the search() will return a match Object. Otherwise, the search() will return None.
As a result, we can print out the lines that start with a sub-string when re.search() tells us that it has found the pattern in the string being provided, which is the value of the variable 'line' in this case.
#Implementing Python's string.startswith() using regular expression
# import the regular expression module
import re
# allow users to specify what string to look for as the start of a string
start_str = raw_input("Enter the string that starts a line: ")
# specify the name of the file to search within
file_name = 'my_file.txt'
# open the file for reading
file_h = open(file_name, 'r')
# define the pattern
# ^ : start of the string
# {} : place holder for start_str
# ^{} -> ^start_str
# -> pattern: a string that starts with the content stored in start_str
# if You want to specify a word, followed by a space, that starts a string
# leave a space after the placeholder: '^{} '
pattern_str = '^{}'.format(start_str)
# read the file line by line
for line in file_h:
# optional: strip the line
line = line.strip()
# search for sub-string that match the pattern in this string, the 'line'
# this is equivalent to line.startswith(start_str)
if re.search(pattern_str, line):
# found the sub-string that matches the pattern in this string, the 'line'
# print the entire string
print line
else:
# didn't find the pattern in this string
# do nothing
pass
# we are done
The pattern_str stored the pattern we specified using regular expression. The '^' character allows us to specify that we are looking for the pattern at the beginning of a string. The '^{}'.format(start_str) allows us to generate a string that consists of '^' and the content in the start_str variable. In this case, '^{}'.format(start_str) will generate a string '^Hello' when the content/value of the variable start_str is 'Hello'. By using '^{}'.format(start_str), we can dynamically change the pattern based on users' input.
The re.search( pattern_str, some_str) will search for sub-string in some_str that matches the pattern in psttern_str. If a match is found, the search() will return a match Object. Otherwise, the search() will return None.
As a result, we can print out the lines that start with a sub-string when re.search() tells us that it has found the pattern in the string being provided, which is the value of the variable 'line' in this case.
Comments