To filter only uppercase words from a file in Python using regular expressions, you can read the file, apply a regex pattern to identify uppercase words, and then extract or print those words. Here's how you can do it:
Assume you have a text file (example.txt) with some text, and you want to extract uppercase words from it:
import re # Define the regex pattern for uppercase words pattern = r'\b[A-Z]+\b' # Open the file and read its contents with open('example.txt', 'r') as file: text = file.read() # Find all uppercase words in the text using regex uppercase_words = re.findall(pattern, text) # Print or process the extracted uppercase words for word in uppercase_words: print(word) Regex Pattern (\b[A-Z]+\b):
\b: Asserts a word boundary to ensure that we match whole words.[A-Z]+: Matches one or more uppercase letters.\b: Asserts another word boundary to ensure that we match whole words.Reading File:
open('example.txt', 'r'): Opens the file example.txt in read mode.file.read(): Reads the entire contents of the file into the variable text.Finding Uppercase Words:
re.findall(pattern, text): Finds all occurrences of the pattern (\b[A-Z]+\b) in the text variable.uppercase_words will contain a list of all uppercase words found in the file.Printing or Processing:
for loop iterates over uppercase_words and prints each uppercase word. You can modify this part to suit your specific needs (e.g., store in a list, write to another file, etc.).If your example.txt contains:
This FILE contains SOME text WITH UPPERCASE words and lowercase words. Here's ANOTHER line WITH more UPPERCASE WORDS.
The output of the above Python script would be:
FILE SOME UPPERCASE ANOTHER WITH UPPERCASE WORDS
This script efficiently filters out only those words that consist entirely of uppercase letters, respecting word boundaries as defined by \b. Adjust the regex pattern (pattern) if your definition of an uppercase word varies (e.g., including numbers or special characters).
Python regex filter uppercase words from file
import re with open('file.txt', 'r') as file: content = file.read() uppercase_words = re.findall(r'\b[A-Z]+\b', content) Filter uppercase words from text file in Python using regex
import re with open('file.txt', 'r') as file: lines = file.readlines() uppercase_words = [re.findall(r'\b[A-Z]+\b', line) for line in lines] Python regex extract only uppercase words from file
import re with open('file.txt', 'r') as file: content = file.read() uppercase_words = re.findall(r'\b[A-Z]+\b', content) Python filter only uppercase words from file content with regex
import re with open('file.txt', 'r') as file: content = file.read() uppercase_words = re.findall(r'\b[A-Z]+\b', content) Python regex extract uppercase words from file content
import re with open('file.txt', 'r') as file: content = file.read() uppercase_words = re.findall(r'\b[A-Z]+\b', content) Filter only uppercase words from file in Python using regex
import re with open('file.txt', 'r') as file: lines = file.readlines() uppercase_words = [re.findall(r'\b[A-Z]+\b', line) for line in lines] Python regex match only uppercase words in file
import re with open('file.txt', 'r') as file: content = file.read() uppercase_words = re.findall(r'\b[A-Z]+\b', content) Extract uppercase words from file in Python using regex
import re with open('file.txt', 'r') as file: lines = file.readlines() uppercase_words = [re.findall(r'\b[A-Z]+\b', line) for line in lines] Python filter only uppercase words from text file with regex
import re with open('file.txt', 'r') as file: lines = file.readlines() uppercase_words = [re.findall(r'\b[A-Z]+\b', line) for line in lines] Filter uppercase words from file in Python using regex
import re with open('file.txt', 'r') as file: content = file.read() uppercase_words = re.findall(r'\b[A-Z]+\b', content) spring-webclient pdfbox cobol arc4random jetty sampling android-uiautomator zip4j toggle sublimetext3