Vacuum Lab Review

Regular Expressions

find

  • The string find method can be used to get the location of one string in another

Example

sent = "Where is the word 'the'?"

print(sent.find("the"))

split

  • The string split method can be used to split strings

Example

ip = "10.75.123.76"

octets = ip.split(".")

print(octets)

Regular Expressions

  • Available in the re module
  • Provide their own mini language for parsing strings
  • Useful for somewhat advanced string processing tasks

Example

import re

zips = "46012, 46013 46014. 46015"

if re.search("46013", zips):
    print("46013 is present")

Character Matching

  • By default, regular expressions match characters literally
  • Some special character can create more complex matches
  • For example . will match any character

Example

import re

words = "cat dog fox pig"

if re.search(".ox", words):
    print("A word ending in ox was found")

Exercise

Create a regular expression that can match all 5 letter words that start with ā€œcā€