Pacific Beach Drive

Mike's Drive.

Follow me on GitHub

Regular Expressions

Learning Objectives

  • Describe regular expressions
  • Precompile regular expressions for speed
  • Construct regular expressions using literal and special characters
  • Group regular expression patterns for more flexible matching
  • Write regular expression with repeating patterns
  • Write code to extract text
  • Write code to substitute text

What are Regular Expressions?

  • ways to search, extract and substitute patterns in string
  • use literal and special characters
  • Act as a compact mini language

Regular Expressions Advantages

  • works well with strings with patterns
  • more compact code than using string methods , slicing and or indexing easier to maintain Available in other programming languages

String Examples With Patterns

  • License plate numbers
  • phone numbers 1-500-555-1212 415-555-2020 (650)777-5555
  • filenames

The Regular Expression Module

  • Built in module for regular expressions
  • contains methods to search for regex patterns
  • returns a match object
    • Contains details about the matched pattern

Using the Regex Module

import the re module => call search methods with regular expression pattern and string => Match object is returned

Search Method Method

Search Method

re.search(pattern, string)

# return a match object if a regex pattern is found in string 
# return None if not found

Match Method

re.match(pattern, string)

# return a match object if a regex pattern is found at the beginning of string 
# return None if not found

Find All All Method

re.findall(pattern, string)

# return a list of all substrings in string that matches the regex pattern 
# return empty list if nothing is found

Search Example

import re
pattern= 'ware'
s = 'software hardware'
 if re.search(pattern, s):
    print('matched')

Match Object

  • returned when a pattern is found
  • contains additinal information about a match, such as groups
  • group() method returns actual pattern that was matched

Match Object Example

import re
pattern= 'ware'
s = 'software hardware'
mo = re.search(pattern, s)
 if mo:
    print(mo.group())
    # Output ware