12.13. 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.

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.

Learning Objectives

Students will know and be able to do the following.

Content Objectives:

12.13.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.

Run the code below to see what it prints.

12.13.2. Quantifiers

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

Run the code below to see what it prints.

12.13.3. Character Classes

There are ways to match particular types of characters. You have already seen \d which matches a digit (0-9).

Run the code below to see what it prints.

Note

Since '\b' is also a backspace in Python if you use \b in a regular expression you must use a raw string r"string".

Run the code below to see what it prints.

12.13.4. 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.

Run the code below to see what it prints.

12.13.5. Greedy and Non-Greedy Matching

Matching is usually greedy.

Run the code below to see what it prints.

12.13.6. Specifying What to Extract

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

Run the code below to see what it prints.

You have attempted of activities on this page