Skip to main content

More Regular Expressions: Using Groups to Process Text

Hands-On Lab

 

Photo of Elle Krout

Elle Krout

Content Team Lead in Content

Length

00:30:00

Difficulty

Intermediate

The use of classes and grouping within regular expressions allows us to fine-tune how we manipulate and reference our text. Capturing groups, in particular, provide us the opportunity to take parts of our match and use them not only in the expression itself but within our greater code or command. In this learning activity, it is necessary to use groups to retrieve the needed parts of the expression, then process that output so humans can understand and make use of the data.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

More Regular Expressions: Using Groups to Process Text

Introduction

The use of classes and grouping within regular expressions allows us to fine-tune how we manipulate and reference our text. Capturing groups, in particular, provide us the opportunity to take parts of our match and use them not only in the expression itself but within our greater code or command. In this learning activity, it is necessary to use groups to retrieve the needed parts of the expression, then process that output so humans can understand and make use of the data.

Solution

Begin by logging in to the lab server using the credentials provided on the hands-on lab page:

ssh cloud_user@PUBLIC_IP_ADDRESS

Create referrers.txt

Generate a file that contains the page and referrer, pulled from Apache access logs.

Perl One-Liner

The access logs can be parsed using a Perl one-liner:

perl -lne '/GET (/w+?) HTTP/1.0" 200 dddd "https?://(?:www.)?(w[a-zA-Z0-9-]*.w{2,63}(?:/w[a-zA-Z0-9-]*)?)/ && print "Page: $1nReferrer: $2n"' access-logs > referrers.txt

Ensure referrers.txt contains page and referrer content

Ensure the referrers.txt file is formatted as dictated by the instructions.

View the contents of our referrers.txt file to ensure it is formatted properly:

cat referrers.txt

Conclusion

Congratulations — you've completed this hands-on lab!