Text Data Processing and Analysis Using Regular Expressions
The project focuses on harnessing the power of Regex to extract, preprocess, and analyze valuable information from raw data. Develop a comprehensive text data processing pipeline that utilizes regular expressions to achieve the following goals: Clean and preprocess text data. Extract structured information and pattern recognition. The project will involve Python programming and the use of a library as 're' for regular expression.
Regular Expression
Importing regex library for Python
import re
Find all numbers in the file
Use regular expression to find all numbers contain in the text.
numbers = re.findall(r'\d+', contents)
Output:
['3036', '7209', '4497', '6702', '8454', '7449', '3665', '7936', '9772', '7114', '956', '2564', '8003', '1704', '3816', '6662', '5858', '7777', '6482', '9634', '8805', '7123', '9703', '4676', '6373', '2834', '7221', '2981', '5415', '6872', '4806', '5460', '8533', '3538', '9663', '8001', '9795', '8752', '1117', '5349', '4509', '1004', '9258', '4183', '4034', '3342', '3482', '8567', '1052', '8135', '5561', '517', '1218', '4', '8877', '8062', '1720', '279', '2054', '801', '918', '8687', '7073', '1865', '7084', '2923', '63', '8824', '1079', '5801', '5047', '5', '2572', '5616', '171', '3062', '9552', '7655', '829', '6096', '2312', '6015', '7100', '9548', '2727', '1792', '8402', '4', '3', '42']
The line only contains a number
For each line use a regular expression to find if this line is a number.
re.match(r'^\d+$', line.strip()):
Output:
['7114', '9634', '2834', '4806', '5', '42']
Searching ULR with regex.
Searching in text-only that contains URL, the word should begin with www. / https or http.
url_pattern = r'\b(?:https?://|www\.)\S+\b' matches = re.finditer(url_pattern, contents)
Output:
['www.py4e.com', 'www.python.org', 'http://www.py4e.com/code3']
Starts with function
For each line, find if starts with a pattern, using rstrip and startswith methods.
line.rstrip() if line.startswith("Terminology:"):
Output:
['Terminology: interpreter and compiler\n']