101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    Python

    Receive aemail containing the next unit.
    • Refreshing Python Basics
      • 1.1Python Data Structures
      • 1.2Syntax and Semantics
      • 1.3Conditionals and Loops
    • Introduction to Object-Oriented Programming
      • 2.1Understanding Class and Objects
      • 2.2Design Patterns
      • 2.3Inheritance, Encapsulation, and Polymorphism
    • Python Libraries
      • 3.1Numpy and Matplotlib
      • 3.2Pandas and Seaborn
      • 3.3SciPy
    • Handling Files and Exception
      • 4.1Reading, writing and manipulating files
      • 4.2Introduction to Exceptions
      • 4.3Handling and raising Exceptions
    • Regular Expressions
      • 5.1Introduction to Regular Expressions
      • 5.2Python’s re module
      • 5.3Pattern Matching, Substitution, and Parsing
    • Databases and SQL
      • 6.1Introduction to Databases
      • 6.2Python and SQLite
      • 6.3Presentation of Data
    • Web Scraping with Python
      • 7.1Basics of HTML
      • 7.2Introduction to Beautiful Soup
      • 7.3Web Scraping Case Study
    • Python for Data Analysis
      • 8.1Data cleaning, Transformation, and Analysis using Pandas
      • 8.2Data visualization using Matplotlib and Seaborn
      • 8.3Real-world Data Analysis scenarios
    • Python for Machine Learning
      • 9.1Introduction to Machine Learning with Python
      • 9.2Scikit-learn basics
      • 9.3Supervised and Unsupervised Learning
    • Python for Deep Learning
      • 10.1Introduction to Neural Networks and TensorFlow
      • 10.2Deep Learning with Python
      • 10.3Real-world Deep Learning Applications
    • Advanced Python Concepts
      • 11.1Generators and Iterators
      • 11.2Decorators and Closures
      • 11.3Multithreading and Multiprocessing
    • Advanced Python Concepts
      • 12.1Generators and Iterators
      • 12.2Decorators and Closures
      • 12.3Multithreading and Multiprocessing
    • Python Project
      • 13.1Project Kick-off
      • 13.2Mentor Session
      • 13.3Project Presentation

    Regular Expressions

    Pattern Matching, Substitution, and Parsing with Regular Expressions in Python

    sequence of characters that forms a search pattern

    Sequence of characters that forms a search pattern.

    Regular expressions (regex) are a powerful tool in Python, used for pattern matching, substitution, and parsing in strings. This unit will delve into these three key applications of regex.

    Pattern Matching

    Pattern matching is one of the most common uses of regular expressions. It involves identifying whether a particular pattern exists within a given string or finding all instances of a pattern in a string.

    Python's re module provides several functions for pattern matching, including match(), search(), and findall().

    • match(): This function checks for a match only at the beginning of the string.
    • search(): This function searches the string for a match and returns a match object if found.
    • findall(): This function returns all non-overlapping matches of a pattern in a string as a list of strings.

    Here's an example of pattern matching using findall():

    import re text = "The rain in Spain" x = re.findall("ai", text) print(x) # Output: ['ai', 'ai']

    Substitution

    Substitution is another common use of regular expressions. It involves replacing parts of a string that match a particular pattern.

    The sub() function in the re module is used for substitution. It replaces all occurrences of the pattern in the string with a substitute and returns the modified string.

    Here's an example of substitution:

    import re text = "The rain in Spain" x = re.sub("ai", "oo", text) print(x) # Output: "The roon in Spoon"

    Parsing

    Parsing is a more complex application of regular expressions. It involves extracting specific information from a string by identifying patterns.

    For example, you might want to extract all email addresses from a text. You can do this by defining a pattern that matches the structure of an email address and using the findall() function to extract all matches.

    Here's an example of parsing:

    import re text = "Contact us at: info@example.com, sales@example.net" emails = re.findall(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', text) print(emails) # Output: ['info@example.com', 'sales@example.net']

    In conclusion, regular expressions are a powerful tool for pattern matching, substitution, and parsing in Python. With a solid understanding of regex, you can perform complex string manipulations with ease.

    Test me
    Practical exercise
    Further reading

    Hey there, any questions I can help with?

    Sign in to chat
    Next up: Introduction to Databases