Search⌘ K
AI Features

Solution Explanations: Regular Expressions

Explore how to apply regular expressions in text preprocessing by extracting IP addresses, timestamps, and counting specific words. Understand patterns, quantifiers, and boundaries to enhance your text data handling skills using Python.

Solution 1: Shorthand character classes

Here’s the solution:

Python 3.8
import pandas as pd
import re
ip_addresses_df = pd.read_csv('practice_logs.csv')
ip_addresses_df = ip_addresses_df['LogText'].str.extract(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
print(ip_addresses_df)

Let’s go through the solution explanation:

  • Line 5: We extract IP addresses using the r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' regular expression that ...