Featured Post

14 Top Data Pipeline Key Terms Explained

Image
 Here are some key terms commonly used in data pipelines 1. Data Sources Definition: Points where data originates (e.g., databases, APIs, files, IoT devices). Examples: Relational databases (PostgreSQL, MySQL), APIs, cloud storage (S3), streaming data (Kafka), and on-premise systems. 2. Data Ingestion Definition: The process of importing or collecting raw data from various sources into a system for processing or storage. Methods: Batch ingestion, real-time/streaming ingestion. 3. Data Transformation Definition: Modifying, cleaning, or enriching data to make it usable for analysis or storage. Examples: Data cleaning (removing duplicates, fixing missing values). Data enrichment (joining with other data sources). ETL (Extract, Transform, Load). ELT (Extract, Load, Transform). 4. Data Storage Definition: Locations where data is stored after ingestion and transformation. Types: Data Lakes: Store raw, unstructured, or semi-structured data (e.g., S3, Azure Data Lake). Data Warehous...

Python Regex: The 5 Exclusive Examples

 Regular expressions (regex) are powerful tools for pattern matching and text manipulation in Python. Here are five Python regex examples with explanations:


Regular expression examples


01 Matching a Simple Pattern


import re


text = "Hello, World!"

pattern = r"Hello"

result = re.search(pattern, text)

if result:

    print("Pattern found:", result.group())

Output:


Output:

Pattern found: Hello

This example searches for the pattern "Hello" in the text and prints it when found.


02 Matching Multiple Patterns


import re


text = "The quick brown fox jumps over the lazy dog."

patterns = [r"fox", r"dog"]

for pattern in patterns:

    if re.search(pattern, text):

        print(f"Pattern '{pattern}' found.")

Output:


Pattern 'fox' found.

Pattern 'dog' found.

It searches for both "fox" and "dog" patterns in the text and prints when they are found.


03 Matching Any Digit

 

import re


text = "The price of the product is $99.99."

pattern = r"\d+"

result = re.search(pattern, text)

if result:

    print("Price:", result.group())

Output:


Price: 99

This example extracts digits (numbers) from the text.


04 Matching Email Addresses


import re


text = "Contact us at support@example.com or info@example.org."

pattern = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b"

emails = re.findall(pattern, text)

for email in emails:

    print("Email:", email)

Output:

Email: support@example.com

Email: info@example.org

It extracts email addresses from the text using a common email pattern.


05. Replacing Text

 

import re

text = "Please visit our website at http://www.example.com."

pattern = r"http://www\.[A-Za-z]+\.[A-Za-z]+"

replacement = "https://www.example.com"

updated_text = re.sub(pattern, replacement, text)

print("Updated Text:", updated_text)

Output:

Updated Text: Please visit our website at https://www.example.com.

This example replaces a URL with a different URL in the text.


These are just a few examples of what you can do with regular expressions in Python. Regex is a versatile tool for text processing, and you can create complex patterns to match specific text structures or extract information from text data.

Comments

Popular posts from this blog

How to Fix datetime Import Error in Python Quickly

SQL Query: 3 Methods for Calculating Cumulative SUM

Big Data: Top Cloud Computing Interview Questions (1 of 4)