Practical 4

Regular expressions

  1. Navigate to https://regex101.com.

  2. Copy and paste the text below into the TEST STRING page on regex101.

    Luke Skywalker
    C-3PO
    R2-D2
    Darth Vader
    Leia Organa
    Owen Lars
    Beru Whitesun Lars
    R5-D4
    Biggs Darklighter
    Obi-Wan Kenobi
    Anakin Skywalker
    Wilhuff Tarkin
    Chewbacca
    Han Solo
  3. Identify regular expressions to match:

    1. Each character’s first name.
    2. Each character’s last name.
    3. All names beginning with L.
    4. Each character’s first and last name as match groups.
TipTips

Use the following hints to define your expressions:

  • ^ = start of line
  • $ = end of line
  • \b = word boundary
  1. ^[A-Za-z0-9-]+
  2. [A-za-z0-9-]+$
  3. ^L[\w]+
  4. (^[\w-]+)[ ]*([\w-]*), although this doesn’t work for characters with middle names.