12.15. Group Work: More 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

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:

12.15.1. Using a logical “or”

What if you want to match a month from 1 to 12 in MM/DD/YYYY? You can’t use [1-12] since it matches a character at a time. You have to match either a digit from 1 to 9 or a 1 followed by 0, 1, or 2. To use a logical or to match one of two expressions use (left|right). This will match either the expression on the left or the one on the right.

Run the code below to see what it prints.

12.15.2. Specifying What to Extract - Matching Groups

There are times when you want to return just part of what was matched.

Run the code below to see what it prints.

Note

Parentheses are used to define a capture group - only what is in the parentheses will be returned.

12.15.3. Specifying What to Extract - Non-Matching Groups

What if we need the parentheses because we are using a logical or but want the whole match to be returned? We can add a “?:” after the first parenthesis to group items for the logical or but return the entire match.

Run the code below to see what it prints.

Another approach is to enclose everything in a set of outer parentheses if you have any inner parentheses.

Run the code below to see what it prints.

12.15.4. Boundary or Anchor Characters

Run the code below to see what it prints.

Run the code below to see what it prints.

Note

Since ‘$’ is an anchor character if you want to match a ‘$’ use ‘\$’.

Run the code below to see what it prints.

Note

Since ‘\b’ usually represents a backspace in a Python string you must use ‘r’ before the string to treat it as a raw string. You only need to add the r in front of the string if the expression has a ‘\b’ in it.

12.15.5. Negating a Character Set

You can negate a character set using the ‘^’ after the ‘[‘.

Run the code below to see what it prints.

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.

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.

You have attempted of activities on this page