Skip to main content

Worksheet 12.14 Group Work: Regular Expressions (Regex)

It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see https://cspogil.org/Home.

Note 12.14.1.

If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.
Learning Objectives
Students will know and be able to do the following.
Content Objectives:

Subsection 12.14.1 Regex Methods

Two of the methods that you can use with regular expressions are search and findall. Note that you must import re to use these.

Activity 12.14.1.

Run the code below to see what it prints.

Activity 12.14.2.

Activity 12.14.3.

Activity 12.14.4.

Subsection 12.14.2 Quantifiers

You can specify how many items to match using quantifiers. They refer to the item to their left. The quantifiers are ?, +, *, {n}, and {n,m}.

Activity 12.14.5.

Run the code below to see what it prints.

Activity 12.14.6.

How many c’s must there be in a row for c{2} to match at least part of the string?
  • 0 to many
  • No, this would be ’c*’
  • 0 to 2
  • No, this would be just ’c’
  • exactly 2
  • No, it will match strings that have more than 2 c’s in a row.
  • 2 or more
  • This will match 2 c’s but there can be more in the string.

Activity 12.14.7.

Activity 12.14.8.

Subsection 12.14.3 Character Sets

You can use [] to specify that you need to match any one item in the [].

Activity 12.14.9.

Run the code below to see what it prints.

Activity 12.14.10.

What does [ea] mean?
  • Match either an ’e’ or ’a’ one time
  • It will match one of the items listed in []
  • Match ’ae’ one time
  • It will match one of the items listed in []
  • Match either an ’e’ or ’a’ one to many times
  • This would be true if it was [ae]+
  • Match ’ae’ one to many times
  • This would be true if it was (ae)+

Subsection 12.14.4 Character Ranges

You can specify a range of items to match.

Activity 12.14.11.

Run the code below to see what it prints.

Activity 12.14.12.

What does [0-9.]+ mean?
  • Match any digit or period one or more times
  • Items in the [] match themselves and are not treated as special characters other than ’-’
  • Match any digit or anything that isn’t a new line one or more times
  • The period in a [] just means match a period
  • Match any digit or period zero to many times
  • The + outside of the [] means match one or more
  • Match any digit or anything that isn’t a new line zero to many times
  • The period in a [] just means match a period and the + means match one or more times

Activity 12.14.13.

What does [^0-9.]+ mean?
  • Match anything other than 0-9 and a period zero to one times
  • The + means one to many times
  • Match anything other than 0-9 and a period one to many times
  • Correct!
  • Match ^ or 0-9 or a period zero to one times
  • The ^ negates the items
  • Match ^ or 0-9 or a period one to many times
  • The ^ negates the items

Subsection 12.14.5 Character Classes

Activity 12.14.14.

Run the code below to see what it prints.

Activity 12.14.15.

Activity 12.14.16.

Run the code below to see what it prints.

Activity 12.14.17.

Subsection 12.14.6 Escaping Special Characters

If you want to match something that is normally a special character in regex you must escape it by adding a \ in front of it.

Activity 12.14.18.

Run the code below to see what it prints.

Activity 12.14.19.

How many items will be in the list that the following code prints?
import re
str = "302.33 64.52 204.24 532.2 1.23 323.320"
res = re.findall("\d{3}\.\d{2}",str)
print(res)
  • 1
  • It will match three digits followed by a period and then 2 digits
  • 2
  • It will match three digits followed by a period and then 2 digits
  • 3
  • It will match three digits followed by a period and then 2 digits
  • 4
  • It will match three digits followed by a period and then 2 digits

Subsection 12.14.7 Greedy and Non-Greedy Matching

Matching is usually greedy.

Activity 12.14.20.

Run the code below to see what it prints.

Activity 12.14.21.

If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share answers.
You have attempted of activities on this page.
The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.