In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "Python how to use regular expression to achieve search function". In daily operation, I believe that many people have doubts about how to use regular expression to achieve search function in Python. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the question of "how to use regular expression to achieve search function in Python". Next, please follow the editor to study!
The topics are as follows:
Write a program that opens all the .txt files in the folder and finds all lines that match the regular expressions provided by the user. The results should be printed on the screen.
The ideas are as follows:
What the program needs to do is as follows:
Traverse the folder to get all .txt file names
Open all .txt files and use regular expressions for pattern matching
The results of the search are displayed on the screen
What the code needs to do is as follows:
Import re,os Modul
Define a regular expression function
Function for regular expression matching and returns a list of rows where the match is located
For calls os.listdir (path) to generate a list of .txt file names
The for loop opens all .txt files
The user enters the string to find
The for loop traversal function returns the result.
Output the results to the screen
The code is as follows: import os, redef fileRex (inputStr,txtLines): txtRex = re.compile (r'{0} '.format (inputStr)) # regular expression object blockList = [] I = 0 for t in txtLines: try: mo1 = txtRex.search (t) mo1.group () # search () method matching successfully generates match object, and group () returns the matching object, which means that this behavior requires a search result. Add to the list I + = 1 # count blockList.append (t) except: continue # search () method does not match will report an error, the program crashes, so you need except plus continue Enter the next loop print ("number of patterns matched:" I) return blockListnameList = [] # create a file name list file = input ("Please enter the absolute path of the folder (e.g. C:\\ Users\\ Administrator\\ Desktop\\ exam):") #\\ escape character is equivalent to\ for fileName in os.listdir (file): # os.listdir () method traversal folder p = 0 if fileName.endswith ('.txt'): # enswith method To determine the end of the string, check whether the file name suffix is .txt text file nameList.append (fileName) # added to the file name list p + = 1 else: continueprint ("number of text files found: {0}! The file is: {1} ".format (pdepartment nameList) txtLines = [] # create a list of lines of text for i in nameList: txtFile = open ('{0}\\ {1} '.format (file,i)) # open function creates a file object txtLines + = txtFile.readlines () # readlines returns a list List returned by the + connection print ("text file line list:\ n", txtLines) # output text line list inputStr = input ("Please enter the string you want to find:") for k in fileRex (inputStr,txtLines): # call the function Loop through the returned list k = k.strip ('\ n') # remove the spaces in the list string print (the lines of text with characters'{0} 'are: {1} ".format (inputStr,k)) # output the search result as follows:
At this point, the study on "how Python uses regular expressions to achieve lookup" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.