Regex Tester (Django). Stage 3/4

Testing regexes

Report a typo

Description

Our service receives input, but nothing happens. Let's change it! This time, users should get a response from the service after filling up and submitting the form.

Objectives

Our main task is to determine whether the pattern matches a string provided by users. It's considered good practice to automate tedious work to the most possible extent. Besides, why match the pattern manually if we have a module that has been created exclusively for this task?

Your task is to create a handler for the form using the re module. First, you should import this module to get a regex and a string from a POST request in a view function. Thereafter, the handler tries to match the submitted regex with the string and show the result to users. At this stage, it should return an HttpResponse from django.http with True or False.

Be careful! The matching method does not return a boolean by itself. In this stage, use HttpResponse only to check the result.

After testing a pattern, the service must save it to the database. Use the Record model that we have created earlier. Remember, the result field is boolean. The form method must be POST.

Examples

Example 1: checking the username

A user entered ^[a-z0-9_-]{3,16}$ to see whether an alphanumeric string matches a username of 3-16 symbols; as a string, the user enters thrawn_66:

Regex testing tool: checking the username

Service output:

Regex testing tool: service output is True

Example 2: checking the age

A user wants to check the age. The first input is [\d]{1,2}$. It matches positive numbers of 1-2 symbols:

Regex testing tool: checking the age

As the second field is of the string type, twenty would produce False:

Regex testing tool: service output is False

Write a program
IDE integration
Checking the IDE status
___

Create a free account to access the full topic