finished project of chapter 8
This commit is contained in:
@@ -6,9 +6,16 @@ regex_search.py - opens all .txt files in a folder and searches for any line
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
target_folder = input("Enter the target foulder:\n")
|
target_folder = input("Enter the target foulder:\n")
|
||||||
os.chdir(target_folder)
|
os.chdir(target_folder)
|
||||||
target_files = os.listdir(target_folder)
|
target_files = os.listdir(target_folder)
|
||||||
target_files = [target for target in target_files if target.endswith('.txt')]
|
target_files = [target for target in target_files if target.endswith('.txt')]
|
||||||
print(target_files)
|
regex = re.compile(input('Enter the regular expression:\n'))
|
||||||
|
for file_name in target_files:
|
||||||
|
file = open(file_name)
|
||||||
|
for line in file.readlines():
|
||||||
|
words = regex.findall(line)
|
||||||
|
if words:
|
||||||
|
print(file_name + ": " + str(words))
|
||||||
|
|||||||
13
practice_projects/regex_test.txt
Normal file
13
practice_projects/regex_test.txt
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
Welcome to RegExr v2.1 by gskinner.com, proudly hosted by Media Temple!
|
||||||
|
|
||||||
|
Edit the Expression & Text to see matches. Roll over matches or the expression for details. Undo mistakes with ctrl-z. Save Favorites & Share expressions with friends or the Community. Explore your results with Tools. A full Reference & Help is available in the Library, or watch the video Tutorial.
|
||||||
|
|
||||||
|
Sample text for testing:
|
||||||
|
abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
|
||||||
|
0123456789 _+-.,!@#$%^&*();\/|<>"'
|
||||||
|
12345 -98.7 3.141 .6180 9,000 +42
|
||||||
|
555.123.4567 +1-(800)-555-2468
|
||||||
|
foo@demo.net bar.ba@test.co.uk
|
||||||
|
www.demo.com http://foo.co.uk/
|
||||||
|
http://regexr.com/foo.html?q=bar
|
||||||
|
https://mediatemple.net
|
||||||
Reference in New Issue
Block a user