# Regular Expressions Worksheet

Use this table to help you remember the metacharacters used in regular expressions:

Metacharacter Meaning
. matches any single character
* matches zero or more occurrences of the previous RE
+ matches one or more occurrences of the preceding RE
? matches zero or one occurrences of the preceding RE
| matches RE before or RE after bar (logical OR)
^ means beginning of line (“^D” matches all strings starting with D)
$ means end of line (“d$” matches all strings ending with d)
\ used to quote other metacharacters
[ and ] indicate a set/range of characters. Any characters in the set will match. ^ before the set means “not”. - between characters indicates a range
( and ) used to group REs when using other metacharacters.
{ and } specify the number of repetitions of an RE

## Question 1

A language L is defined by the alphabet {a, b, c} together with the regular expression (a|c)+bb

Give two examples of valid strings in L.

## Question 2

Here are some informal descriptions of languages. Write down a regular expression to define each language formally. The alphabet for the languages is {0, 1}. Note any ambiguity in the informal descriptions.

1. all possible binary strings
2. strings in the language that consist entirely of 1s and do not include the empty string
3. strings in the language that start with a single 1 which may be followed by zero or more 0s
4. strings in the language that always start with either 110 or 011. The first three symbols may be followed by zero or more 0s or 1s in any order.
5. any non-empty string in the language as long as it ends with a 0
6. any non-empty string that starts and ends with two 1s is a valid string in the language.

## Question 3

Write down the strings defined by the regular expression b[ea]d.

## Question 4

Write down the strings defined by the regular expression 10*1.

## Question 5

Write down the strings defined by the regular expression 10+1.

## Question 6

Place a Y or N to show which of the following strings match this regular expression:

be[ea]n

String Match (Y or N)
ben
been
bean
beean

## Question 7

Place a Y or N to show which of the following strings match this regular expression:

be?ad

String Match (Y or N)
bed

## Question 8

Place a Y or N to show which of the following strings match this regular expression:

10(1)*01

String Match (Y or N)
1001
100101
10101
1011101

## Question 9

Place a Y or N to show which of the following strings match this regular expression:

\d+(-|\s)\d+

String Match (Y or N)
01296-433006
01296--433006
01793 234589
01793234589

## Question 10

The word "licence" has been spelled in the following ways in a piece of text: licence, license, lisence, lisense. Write a regular expression that will find all instances of the word "license" spelt in these different ways.

## Question 11

Create a regular expression to validate website addresses which always begin with www., such as www.pythonschool.net.

by Sue Sentance.
30 August 2014.
updated on 07 September 2014.