Python Regular Expressions Basics

PythonPythonBeginner
Practice Now

Introduction

Welcome to Hogwarts School of Witchcraft and Wizardry, where the magical world's brightest minds gather to delve into the depths of sorcery. Among these corridors and hidden chambers, lies a challenge even the most skilled wizards find perplexing: mastering the arcane art of Python Regular Expressions.

In this enchanted setting, we meet our protagonist, a gifted wand craftsman known for creating the most intricate and powerful wands. These wands are not just tools but carry within them lore and secrets of the ancient magic world. To keep up with the high demands of customization and intricacies, the wand craftsman has turned to Python Regular Expressions to streamline the carving runes and enchantment phrases.

The goal is clear: learn and employ the powerful spells of Python Regular Expressions to help our wand craftsman organize and process magical texts and formulas more efficiently. Can you rise to this challenge and become a Regular Expressions wizard?


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python/AdvancedTopicsGroup -.-> python/regular_expressions("`Regular Expressions`") subgraph Lab Skills python/regular_expressions -.-> lab-271586{{"`Python Regular Expressions Basics`"}} end

Understand Basic Patterns

In this step, you will get introduced to the basic patterns used in Python Regular Expressions. With the right pattern, you can match, search, and filter text just like casting a precise spell. You'll practice creating a basic regular expression to identify simple rune sequences in wand designs.

Let's start by opening a file named rune_sequences.py in the ~/project directory and write a regular expression to find all the vowel combinations in a given piece of text.

import re

## Sample text with imaginary rune sequences
text = "aeiou aci aeiooo cuii exiovi"

## Regular expression pattern to match all vowel combinations
pattern = r"[aeiou]+"

## Finding all matches using the 'findall' method
matches = re.findall(pattern, text)

print("Vowel combinations found:", matches)

Run the above code in your terminal and observe the output.

python rune_sequences.py

The expected output should list all combinations of vowels like so:

Vowel combinations found: ['aeiou', 'a', 'i', 'aeiooo', 'uii', 'e', 'io', 'i']

Extracting Wand Core Elements

Now that you've mastered the basic pattern matching, let's move to something more advanced. The wand craftsman needs to extract core materials from a list. Each wand core is a compound word, usually prefixed with a magical creature and suffixed with a magical substance.

Open a file named wand_cores.py in the ~/project directory and write a regular expression pattern to match and list wand cores from the provided text.

import re

## Text with mixed information, including wand cores
text = "dragonheartstring phoenixfeather unicornhair basiliskvenom trollwhisker"

## Regular expression pattern to match wand cores (compound words)
pattern = r"\b\w+heartstring|\b\w+feather|\b\w+hair|\b\w+venom|\b\w+whisker"

## Finding all matches using the 'findall' method
wand_cores = re.findall(pattern, text)

print("Wand cores extracted:", wand_cores)

Run the above code and check the list of wand cores.

python wand_cores.py

The expected output should display the wand cores found in the text:

Wand cores extracted: ['dragonheartstring', 'phoenixfeather', 'unicornhair', 'basiliskvenom', 'trollwhisker']

Validate Enchantment Phrases

As the final challenge, we will validate the phrases used to enchant the wands. Phrases must follow a strict pattern: They must start with a magic word, followed by a colon, and then a series of comma-separated magical parameters or incantations. A valid enchantment phrase looks like 'Lumos:maxima,solemnly,nova'.

Open enchantment_validator.py in the ~/project directory, and write a function that uses a regular expression to validate a list of enchantment phrases.

import re

def validate_enchantment(phrase):
    ## Regular expression pattern to match valid enchantment phrases
    pattern = r"^[A-Za-z]+:(?:[A-Za-z]+,)*[A-Za-z]+$"
    if re.fullmatch(pattern, phrase):
        return True
    else:
        return False

## List of phrases to validate
phrases = [
    "Lumos:maxima,solemnly,nova",
    "Reducio:shrink,less",
    "Protego:maxima",
    "Alohomora:",
    "Expelliarmus:disarm,fight,duel,"
]

## Validate each phrase
for phrase in phrases:
    result = validate_enchantment(phrase)
    print(f"'{phrase}' is {'valid' if result else 'invalid'}")

Run the code and check the validation results.

python enchantment_validator.py

The expected output should show which phrases are valid:

'Lumos:maxima,solemnly,nova' is valid
'Reducio:shrink,less' is valid
'Protego:maxima' is valid
'Alohomora:' is invalid
'Expelliarmus:disarm,fight,duel,' is invalid

Summary

In this lab, you embarked on a magical journey through the Hogwarts School of Witchcraft and Wizardry to master the spells of Python Regular Expressions. From deciphering basic patterns in rune sequences to extracting wand cores and validating enchantment phrases, you've honed your skills to aid the wand craftsman.

The hope is that through this hands-on approach, you've gained a deeper understanding and appreciation of the power and versatility of regex in text processing. Your journey not only enhanced your coding repertoire but also imbued you with the magic of Python in the world of Regular Expressions.

Other Python Tutorials you may like